C# 从目录中删除只读属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2316308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove readonly attribute from directory
提问by Red Swan
How can I programatically remove the readonly attribute from a directory in C#?
如何以编程方式从 C# 中的目录中删除 readonly 属性?
采纳答案by Darin Dimitrov
var di = new DirectoryInfo("SomeFolder");
di.Attributes &= ~FileAttributes.ReadOnly;
回答by Kyle Trauberman
Here's a good link to examples of modifying file attributes using c#
这是使用 c# 修改文件属性的示例的良好链接
http://www.csharp-examples.net/file-attributes/
http://www.csharp-examples.net/file-attributes/
based on their example, you can remove the Read Only attribute like this (I haven't tested this):
根据他们的示例,您可以像这样删除只读属性(我还没有测试过):
File.SetAttributes(filePath, File.GetAttributes(filePath) & ~FileAttributes.ReadOnly);
回答by cbkadel
If you're attempting to remove the attribute of a file in the file system, create an instance of the System.IO.FileInfo class and set the property IsReadOnly to false.
如果您尝试删除文件系统中文件的属性,请创建 System.IO.FileInfo 类的实例并将属性 IsReadOnly 设置为 false。
FileInfo file = new FileInfo("c:\microsoft.text");
file.IsReadOnly = false;
回答by Pratik Deoghare
Got it finally. ;)
终于搞定了 ;)
class Program
{
static void Main(string[] args)
{
DirectoryInfo di = new DirectoryInfo("c:\test");
FileAttributes f = di.Attributes;
Console.WriteLine("Directory c:\test has attributes:");
DecipherAttributes(f);
}
public static void DecipherAttributes(FileAttributes f)
{
// To set use File.SetAttributes
File.SetAttributes(@"C:\test", FileAttributes.ReadOnly);
if ((f & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
Console.WriteLine("ReadOnly");
// To remove readonly use "-="
f -= FileAttributes.ReadOnly;
if ((f & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
Console.WriteLine("ReadOnly");
else
Console.WriteLine("Not ReadOnly");
}
}
回答by Guakamole2
Using the -=
assignment operator is dangerous for two reasons:
1) It works ONLY IF the ReadOnly
attribute is set, thus a test is required beforehand.
2) It is performing a subtract operation, which is not is not the best choice when working with binary flags. The subtract operation works if condition 1 (above) is true, but additional subtract operations will ALTER OTHER BITS in the FileAttributes
field!
使用-=
赋值运算符是危险的,原因有两个:
1) 只有在ReadOnly
设置了属性时它才起作用,因此需要事先进行测试。
2)它正在执行减法运算,这在使用二进制标志时不是最好的选择。如果条件 1(以上)为真,则减法运算有效,但额外的减法运算将改变该FileAttributes
字段中的其他位!
Use &= ~FileAttributes.ReadOnly;
to remove ReadOnly
flag.
使用&= ~FileAttributes.ReadOnly;
删除ReadOnly
标志。
Use |= FileAttributes.ReadOnly;
to apply ReadOnly
flag.
使用|= FileAttributes.ReadOnly;
申请ReadOnly
标志。
回答by daniellmb
Setting Attributes to FileAttributes.Normal
worked for me on both foldersand files.
将 Attributes 设置FileAttributes.Normal
为对文件夹和文件都适用。
回答by Nandini B
public static void DeleteDirectory(string path)
{
var directory = new DirectoryInfo(path)
{ Attributes =FileAttributes.Normal };
foreach (var info in directory.GetFileSystemInfos("*", SearchOption.AllDirectories))
{
info.Attributes = FileAttributes.Normal;
}
directory.Delete(true);
}