您的位置:首页 > 其它

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

2009-10-15 00:36 246 查看
關於 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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: