當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。