如何清除 .NET 中文件上的只读标志并保持其余部分完好无损?
user113476
问问题
386 次
2 回答
4
你不能这样做:
FileInfo f = new FileInfo("yourfile.txt");
f.IsReadOnly = false;
还是我错过了什么?
于 2009-12-23T21:49:13.660 回答
2
我将获取文件的 FileInfo 实例,然后将 IsReadOnly 属性设置为 false(根据此处的文档:http: //msdn.microsoft.com/en-us/library/system.io.fileinfo.isreadonly.aspx ):
new FileInfo("path").IsReadOnly = false;
如果你坚持在 File 类上使用静态 GetAttributes 和 SetAttributes 方法,你可以简单地这样做:
File.SetAttributes("path",
File.GetAttributes("path") & ~FileAttributes.ReadOnly);
当您想要清除位图上的标志时,一般模式是获取标志的值(在本例中为 FileAttributes.ReadOnly),反转它(使用 ~ 运算符),然后将反转的值应用于包含的值各种标志(在本例中为 File.GetAttributes("path"))。
于 2009-12-23T21:48:41.067 回答