📜  C#中的File.GetAttributes()方法与示例

📅  最后修改于: 2021-05-29 15:01:49             🧑  作者: Mango

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

句法:

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

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

path: This is the specified file path.

例外情况:

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

返回值:返回路径上文件的FileAttributes。
下面是说明File.GetAttributes(String)方法的程序。
程序1:在运行下面的代码之前,将创建一个文件file.txt ,其内容如下所示-

file.txt

CSharp
// 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()
    {
        // Specifying 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;
    }
}


CSharp
// 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()
    {
        // Specifying 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 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()
    {
        // Specifying 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.