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


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


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

用法:

public static System.IO.FileAttributes GetAttributes (string path);

参数:该函数接受如下所示的参数:



  • path: This is the specified file path.

异常:

  • ArgumentException:路径为空,仅包含空格或无效字符。
  • PathTooLongException:指定的路径,文件名或两者都超过了system-defined的最大长度。
  • NotSupportedException:路径格式无效。
  • FileNotFoundException:该路径代表一个文件,并且无效,例如在未映射的驱动器上,或者找不到该文件。
  • DirectoryNotFoundException:路径代表目录,并且无效,例如位于未映射的驱动器上或找不到目录。
  • IOException:该文件正在由另一个进程使用。
  • UnauthorizedAccessException:调用者没有所需的权限。

返回值:它返回路径上文件的FileAttributes。

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

程序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.GetAttributes() Method in C# with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。