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


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