如何从文件中删除单个属性(例如ReadOnly)?

假设一个文件具有以下属性: ReadOnly, Hidden, Archived, System我怎样才能删除一个属性? (例如ReadOnly)

如果我使用:

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

它删除所有的属性。

从MSDN :你可以删除任何这样的属性

(但是@Sll的只是ReadOnly的答案只是更好的属性)

 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; } } 

回答关于ReadOnly属性的标题问题:

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

要自己控制任何属性,可以使用File.SetAttributes()方法。 链接也提供了一个例子。

 string file = "file.txt"; FileAttributes attrs = File.GetAttributes(file); if (attrs.HasFlag(FileAttributes.ReadOnly)) File.SetAttributes(file, attrs & ~FileAttributes.ReadOnly); 
 if ((oFileInfo.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) oFileInfo.Attributes ^= FileAttributes.ReadOnly; 

对于单线解决scheme(假设当前用户有权更改提到的文件的属性),我将如何做到这一点:

VB.Net

 Shell("attrib file.txt -r") 

负号表示remover表示只读。 如果你想删除其他属性,你会做:

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

这将删除只读,系统文件,隐藏和存档属性。

如果你想回馈这些属性,这里是如何:

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

顺序无关紧要。

C#

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

参考

  • 谷歌
  • ComputerHope.com – 大量的例子和操作系统的具体说明
  • 微软 – 页面是为XP,但可能适用于更高版本
  • 维基百科 – 特别是特殊部分
 /// <summary> /// Addes the given FileAttributes to the given File. /// It's 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's 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's 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's 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)); } 

例:

 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); } 

用这个:

 private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove) 

在MSDN中阅读详细信息: http : //msdn.microsoft.com/en-us/library/system.io.file.setattributes.aspx