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
Selenium: Drag and Drop from File System to Webdriver
How to Check for Nulls in a Deep Lambda Expression
Sorting an Array of Folder Names Like Windows Explorer (Numerically and Alphabetically) - Vb.Net
How to Call the Parent Version of an Overridden Method? (C# .Net)
Fastest Way to Add New Node to End of an Xml
How to Read Image Pixels' Values as Rgb into 2D Array
Error: Must Create Dependencysource on Same Thread as the Dependencyobject Even by Using Dispatcher
With Rx, How to Ignore All-Except-The-Latest Value When My Subscribe Method Is Running
Passing Command Line Parameters with Visual Studio C#
How Can a Windows Service Determine Its Servicename
How Does C# Choose with Ambiguity and Params
Combine Two Images into One New Image
Implementing the Producer/Consumer Pattern in C#
Adding Elements to an Xml File in C#
Should I Transform Entity (Persistent) Objects to Dto Objects