在.NET去掉文件的只读属性

来源:互联网 发布:算法总结 编辑:程序博客网 时间:2024/05/16 04:17

關於 FileAttributes的所有枚舉值都在這裡:
ReadOnly = 1,
Hidden = 2,
System = 4,
Directory = 0x10,
Archive = 0x20,
Device = 0x40,
Normal = 0x80,
Temporary = 0x100
SparseFile = 0x200,
ReparsePoint = 0x400,
Compressed = 0x800,
Offline = 0x1000,
NotContentIndexed = 0x2000,
Encrypted = 0x4000,

 

我们可以使用 System.IO.File 类的 SetAttributes 方法为一个文件设置相关的属性,如:

// 为文件加上一个只读的属性
FileAttributes attrs = File.GetAttributes("c://a.txt");
File.SetAttributes(
"c://a.txt", attrs | FileAttributes.ReadOnly);


怎么把这个只读属性去掉呢?

// 先把文件的属性读取出来
FileAttributes attrs = File.GetAttributes("c://a.txt");

// 下面表达式中的 1 是 FileAttributes.ReadOnly 的值
   // 此表达式是把 ReadOnly 所在的位改成 0,
attrs = (FileAttributes)((int)attrs & ~(1));

File.SetAttributes(
"c://a.txt", attrs);
原创粉丝点击