c#设置文件及文件夹的属性
2013-12-11 11:05
375 查看
c#中通过FileAttributes枚举来设置文件或文件夹的属性。 FileAttributes 枚举 成员名称 说明 Archive 文件的存档状态。应用程序使用此属性为文件加上备份或移除标记。 Compressed 文件已压缩。 Device 保留供将来使用。 Directory 文件为一个目录。 Encrypted 该文件或目录是加密的。对于文件来说,表示文件中的所有数据都是加密的。对于目录来说,表示新创建的文件和目录在默认情况下是加密的。 Hidden 文件是隐藏的,因此没有包括在普通的目录列表中。 Normal 文件正常,没有设置其他的属性。此属性仅在单独使用时有效。 NotContentIndexed 操作系统的内容索引服务不会创建此文件的索引。 Offline 文件已脱机。文件数据不能立即供使用。 ReadOnly 文件为只读。 ReparsePoint 文件包含一个重新分析点,它是一个与文件或目录关联的用户定义的数据块。 SparseFile 文件为稀疏文件。稀疏文件一般是数据通常为零的大文件。 System 文件为系统文件。文件是操作系统的一部分或由操作系统以独占方式使用。 Temporary 文件是临时文件。文件系统试图将所有数据保留在内存中以便更快地访问,而不是将数据刷新回大容量存储器中。不再需要临时文件时,应用程序会立即将其删除。 看一个简单的例子: /// <summary> /// 设置文件只读 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button2_Click(object sender, EventArgs e) { //设置文件属性 File.SetAttributes(Server.MapPath("~/TextFile.txt"), FileAttributes.ReadOnly); //获取文件属性 FileAttributes MyAttributes = File.GetAttributes(Server.MapPath("~/TextFile.txt")); Literal2.Text = MyAttributes.ToString(); } FileAttributes 具有FlagsAttribute属性,将枚举作为位域(即一组标志)处理。位域通常用于由可组合出现的元素组成的列表,而枚举常数通常用于由互相排斥的元素组成的列表。因此,位域设计为通过按位“或”运算组合来生成未命名的值。 看个例子: /// <summary> /// 设置文件只读加隐藏 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button3_Click(object sender, EventArgs e) { //设置文件属性 File.SetAttributes(Server.MapPath("~/TextFile.txt"), FileAttributes.ReadOnly | FileAttributes.Hidden); //获取文件属性 FileAttributes MyAttributes = File.GetAttributes(Server.MapPath("~/TextFile.txt")); Literal3.Text = MyAttributes.ToString(); } 这样就可以组合设置多个属性了。 如果文件已经存在了,直接设置文件的属性,会移除文件已经存在的其它属性。 可以先获取文件的属性,然后再添加需要设置的属性。 /// <summary> /// 附加只读属性 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button4_Click(object sender, EventArgs e) { //获取文件属性 FileAttributes MyAttributes = File.GetAttributes(Server.MapPath("~/TextFile.txt")); //设置文件属性 File.SetAttributes(Server.MapPath("~/TextFile.txt"), MyAttributes | FileAttributes.ReadOnly); //重新获取文件属性 MyAttributes = File.GetAttributes(Server.MapPath("~/TextFile.txt")); Literal4.Text = MyAttributes.ToString(); } 对于设置文件夹的属性和设置文件一样。 另外还有其它几个设置属性的方法: 这里不一一说明了。参考:http://msdn.microsoft.com/zh-cn/library/system.io.fileattributes.aspx
相关文章推荐
- C#文件操作,父目录,子目录,所有文件、文件夹,属性信息
- VBA 获取和设置(文件和文件夹属性…
- C# 去除文件夹内部所有文件的只读属性,并清空文件夹。
- [Android]如何在编译阶段设置文件夹文件的权限属性(mode ,group, owner)
- C#中,如何获得或设置文件属性(Attributes)
- 公司员工没有管理员权限怎么办 C#文件夹、文件添加权限 C#读取windows系统日志 C#设置环境变量
- C#获取设置文件属性
- windows 中设置文件和文件夹的属性
- C#_FileInfo文件属性类和DirectoryInfo文件夹属性类
- c# 设置属性的样式(选择文件)
- C#操作文件、文件夹(复制、删除、新增、设置只读)-技术&分享
- JAVA基础初探(十二)Map接口及其常用实现类(HashMap)、File类详解(概述、创建、删除、重命名、文件属性读取/设置、遍历文件夹)
- c# 设置文件夹属性
- C#复制文件、设置隐藏,只读属性
- 批处理自动设置指定文件/文件夹属性
- 获取指定文件夹下的所有文件+文件属性的获取与设置+文件夹属性的修改
- C# 去除文件和文件夹的只读属性
- 公司员工没有管理员权限怎么办 C#文件夹、文件添加权限 C#读取windows系统日志 C#设置环境变量
- 如何使用DOS命令attrib设置文件或文件夹的属性
- 文件的打开、复制、写入、创建、删除、获取设置 文件属性 查找文件 遍历文件夹等14.4.26