How to Remove a Single Attribute (E.G. Readonly) from a File

How to remove a single Attribute (e.g. ReadOnly) from a File?

From MSDN: You can remove any attribute like this

(but @sll's answer for just ReadOnly is better for just that attribute)

using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";

// Create the file if it exists.
if (!File.Exists(path))
{
File.Create(path);
}

FileAttributes attributes = File.GetAttributes(path);

if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
// Make the file RW
attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly);
File.SetAttributes(path, attributes);
Console.WriteLine("The {0} file is no longer RO.", path);
}
else
{
// Make the file RO
File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
Console.WriteLine("The {0} file is now RO.", path);
}
}

private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
{
return attributes & ~attributesToRemove;
}
}

Remove readonly attribute from directory

var di = new DirectoryInfo("SomeFolder");
di.Attributes &= ~FileAttributes.ReadOnly;

Removing read only attribute on a directory using C#

You could also do something like the following to recursively clear readonly (and archive, etc.) for all directories and files within a specified parent directory:

private void ClearReadOnly(DirectoryInfo parentDirectory)
{
if(parentDirectory != null)
{
parentDirectory.Attributes = FileAttributes.Normal;
foreach (FileInfo fi in parentDirectory.GetFiles())
{
fi.Attributes = FileAttributes.Normal;
}
foreach (DirectoryInfo di in parentDirectory.GetDirectories())
{
ClearReadOnly(di);
}
}
}

You can therefore call this like so:

public void Main()
{
DirectoryInfo parentDirectoryInfo = new DirectoryInfo(@"c:\test");
ClearReadOnly(parentDirectoryInfo);
}

How to Remove the Readonly attribute of a File MFC

Use SetFileAttributes again to reset the flag:

SetFileAttributes( pszFilename,  
GetFileAttributes(pszFilename) & ~FILE_ATTRIBUTE_READONLY);

Might be worth adding that this method returns 0 if the function fails and you can use GetLastError().

How to Remove ReadOnly Attribute on File Using PowerShell?

You can use Set-ItemProperty:

Set-ItemProperty file.txt -name IsReadOnly -value $false

or shorter:

sp file.txt IsReadOnly $false

Remove read-only attribute from existing files in Inno Setup

There's no built-in mechanism for that in Inno Setup.

But you can do almost anything from Pascal Script using WinAPI. In this case, you want to use SetFileAttributes.

For an example how to use SetFileAttributes from Inno Setup, see:

Inno Setup Code section create hidden file

How do I delete a read-only file?

According to File.Delete's documentation,, you'll have to strip the read-only attribute. You can set the file's attributes using File.SetAttributes().

using System.IO;

File.SetAttributes(filePath, FileAttributes.Normal);
File.Delete(filePath);

how we add or remove readonly attribute from textbox on clicking radion button in cakephp using jquery?

In your Case you can write the following jquery code:

$(document).ready(function(){

$('.staff_on_site').click(function(){

var rBtnVal = $(this).val();

if(rBtnVal == "yes"){
$("#no_of_staff").attr("readonly", false);
}
else{
$("#no_of_staff").attr("readonly", true);
}
});
});

Here is the Fiddle: http://jsfiddle.net/P4QWx/3/



Related Topics



Leave a reply



Submit