How to remove a single attribute (e.g. ReadOnly) from a file?

Let's say the file has the following attributes: ReadOnly, Hidden, Archived, System . How to remove only one attribute? (e.g. ReadOnly)

If I use:

 Io.File.SetAttributes("File.txt",IO.FileAttributes.Normal) 

it removes all attributes.

+72
c # file
Sep 13 2018-11-11T00:
source share
7 answers

From MSDN : you can remove any attribute like this

(but @sll's answer for just ReadOnly is best suited exactly for this 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; } } 
+92
Sep 13 2018-11-11T00
source share

Answering your question in the header regarding the ReadOnly attribute:

 FileInfo fileInfo = new FileInfo(pathToAFile); fileInfo.IsReadOnly = false; 

To gain control over any attribute, you can use the File.SetAttributes() method. The link also gives an example.

+110
Sep 13 '11 at 9:32 a.m.
source share
 string file = "file.txt"; FileAttributes attrs = File.GetAttributes(file); if (attrs.HasFlag(FileAttributes.ReadOnly)) File.SetAttributes(file, attrs & ~FileAttributes.ReadOnly); 
+12
Sep 13 2018-11-11T00:
source share
 if ((oFileInfo.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) oFileInfo.Attributes ^= FileAttributes.ReadOnly; 
+2
Aug 19 '15 at 10:16
source share

To solve a single line (provided that the current user has access to change the attributes of the mentioned file), here's how I do it:

Vb.net

 Shell("attrib file.txt -r") 

a negative sign means remove , and r is read-only. if you also want to remove other attributes, follow these steps:

 Shell("attrib file.txt -r -s -h -a") 

This will remove the Read-Only, System-File, Hidden, and Archive attributes.

if you want to return these attributes, here's how:

 Shell("attrib file.txt +r +s +h +a") 

order doesn't matter.

FROM#

 Process.Start("cmd.exe", "attrib file.txt +r +s +h +a"); 

References

+1
Sep 13 '11 at 9:41
source share
 /// <summary> /// Addes the given FileAttributes to the given File. /// It possible to combine FileAttributes: FileAttributes.Hidden | FileAttributes.ReadOnly /// </summary> public static void AttributesSet(this FileInfo pFile, FileAttributes pAttributes) { pFile.Attributes = pFile.Attributes | pAttributes; pFile.Refresh(); } /// <summary> /// Removes the given FileAttributes from the given File. /// It possible to combine FileAttributes: FileAttributes.Hidden | FileAttributes.ReadOnly /// </summary> public static void AttributesRemove(this FileInfo pFile, FileAttributes pAttributes) { pFile.Attributes = pFile.Attributes & ~pAttributes; pFile.Refresh(); } /// <summary> /// Checks the given File on the given Attributes. /// It possible to combine FileAttributes: FileAttributes.Hidden | FileAttributes.ReadOnly /// </summary> /// <returns>True if any Attribute is set, False if non is set</returns> public static bool AttributesIsAnySet(this FileInfo pFile, FileAttributes pAttributes) { return ((pFile.Attributes & pAttributes) > 0); } /// <summary> /// Checks the given File on the given Attributes. /// It possible to combine FileAttributes: FileAttributes.Hidden | FileAttributes.ReadOnly /// </summary> /// <returns>True if all Attributes are set, False if any is not set</returns> public static bool AttributesIsSet(this FileInfo pFile, FileAttributes pAttributes) { return (pAttributes == (pFile.Attributes & pAttributes)); } 

Example:

 private static void Test() { var lFileInfo = new FileInfo(@"C:\Neues Textdokument.txt"); lFileInfo.AttributesSet(FileAttributes.Hidden | FileAttributes.ReadOnly); lFileInfo.AttributesSet(FileAttributes.Temporary); var lBool1 = lFileInfo.AttributesIsSet(FileAttributes.Hidden); var lBool2 = lFileInfo.AttributesIsSet(FileAttributes.Temporary); var lBool3 = lFileInfo.AttributesIsSet(FileAttributes.Encrypted); var lBool4 = lFileInfo.AttributesIsSet(FileAttributes.ReadOnly | FileAttributes.Temporary); var lBool5 = lFileInfo.AttributesIsSet(FileAttributes.ReadOnly | FileAttributes.Encrypted); var lBool6 = lFileInfo.AttributesIsAnySet(FileAttributes.ReadOnly | FileAttributes.Temporary); var lBool7 = lFileInfo.AttributesIsAnySet(FileAttributes.ReadOnly | FileAttributes.Encrypted); var lBool8 = lFileInfo.AttributesIsAnySet(FileAttributes.Encrypted); lFileInfo.AttributesRemove(FileAttributes.Temporary); lFileInfo.AttributesRemove(FileAttributes.Hidden | FileAttributes.ReadOnly); } 
+1
Aug 13 '14 at 10:32
source share

Use this:

 private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove) 

Read more at MSDN: http://msdn.microsoft.com/en-us/library/system.io.file.setattributes.aspx

0
Sep 13 2018-11-11T00
source share



All Articles