zoukankan      html  css  js  c++  java
  • c#设置文件及文件夹的属性

    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
  • 相关阅读:
    1.python简介
    JSP标准标签库:JSTL
    冒泡排序算法
    中英文金额大写转换器
    递归与斐波那契数列
    web.xml配置文件详解
    Servlet及相关类和接口
    Servlet初始化及处理HTTP请求
    [转]jqGrid 属性、事件全集
    java web 过滤器跟拦截器的区别和使用
  • 原文地址:https://www.cnblogs.com/qq1223558/p/3468900.html
Copyright © 2011-2022 走看看