我们可以使用 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);
關於 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,
本文作者:kuku 来源:http://www.cnblogs.com/kuku/archive/2004/11/08/614
CIO之家 www.ciozj.com 微信公众号:imciow