当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# File.SetAttributes()用法及代码示例


File.SetAttributes(String,FileAttributes)是一个内置的File类方法,用于在指定路径上设置文件的指定文件属性。文件属性是授予或拒绝的某些权限。这些权限适用于访问文件的用户或操作系统。这些属性例如是只读,存档,系统,隐藏等。

用法:

public static void SetAttributes (string path, System.IO.FileAttributes fileAttributes);

参数:此函数接受两个参数,如下所示:



  • path: This is the specified file path.
  • fileAttributes: This is the bitwise combination of the enumeration values.

异常:

  • ArgumentException:路径为空,仅包含空格,无效字符或文件属性无效。
  • PathTooLongException:指定的路径,文件名或两者都超过了system-defined的最大长度。
  • NotSupportedException:路径格式无效。
  • DirectoryNotFoundException:指定的路径无效。
  • FileNotFoundException:找不到文件。
  • UnauthorizedAccessException:该路径指定了一个只读文件。或者当前平台不支持此操作。或路径指定目录。或调用者没有所需的权限。

下面是说明File.SetAttributes(String,FileAttributes)方法的程序。

程序1:在运行下面的代码之前,将创建一个文件file.txt,其内容如下所示:

file.txt

// C# program to illustrate the usage 
// of File.GetAttributes(String) method 
  
// Using System, System.IO 
// and System.Text namespaces 
using System; 
using System.IO; 
using System.Text; 
  
class GFG { 
    public static void Main() 
    { 
        // Specifing a file 
        string path = @"file.txt"; 
  
        // Getting the file attributes 
        FileAttributes attributes = File.GetAttributes(path); 
  
        // Checking if the file is having whether hidden attributes 
  
        // If the file is having hidden attribute then 
        // that attribute will be removed 
        if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden) { 
  
            // Removing the file's hidden attribute 
            attributes = RemoveAttribute(attributes, FileAttributes.Hidden); 
  
            // Calling the SetAttributes() function 
            File.SetAttributes(path, attributes); 
            Console.WriteLine("The {0} file is no longer hidden.", path); 
        } 
        else { 
            // Calling the SetAttributes() function to 
            // set hidden attribute 
            File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden); 
            Console.WriteLine("The {0} file is now hidden.", path); 
        } 
    } 
  
    private static FileAttributes RemoveAttribute(FileAttributes attributes, 
                                          FileAttributes attributesToRemove) 
    { 
        return attributes & ~attributesToRemove; 
    } 
}

输出:

The file.txt file is now hidden.

程序2:最初,没有创建文件。在代码下方,其自身创建了一个文件gfg.txt。

// C# program to illustrate the usage 
// of File.GetAttributes(String) method 
  
// Using System, System.IO 
// and System.Text namespaces 
using System; 
using System.IO; 
using System.Text; 
  
class GFG { 
    public static void Main() 
    { 
        // Specifing a file 
        string path = @"gfg.txt"; 
  
        // Create the file if it does not exist. 
        if (!File.Exists(path)) { 
            File.Create(path); 
        } 
  
        // Getting the file attributes 
        FileAttributes attributes = File.GetAttributes(path); 
  
        // Checking if the file is having whether hidden attributes 
  
        // If the file is having hidden attribute then 
        // that attribute will be removed 
        if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden) { 
  
            // Removing the file's hidden attribute 
            attributes = RemoveAttribute(attributes, FileAttributes.Hidden); 
  
            // Calling the SetAttributes() function 
            File.SetAttributes(path, attributes); 
            Console.WriteLine("The {0} file is no longer hidden.", path); 
        } 
        else { 
  
            // Calling the SetAttributes() function to 
            // set hidden attribute 
            File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden); 
            Console.WriteLine("The {0} file is now hidden.", path); 
        } 
    } 
  
    private static FileAttributes RemoveAttribute(FileAttributes attributes, 
                                          FileAttributes attributesToRemove) 
    { 
        return attributes & ~attributesToRemove; 
    } 
}

输出:

The gfg.txt file is now hidden.



相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 File.SetAttributes() Method in C# with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。