File.GetAttributes 方法

定义

重载

GetAttributes(SafeFileHandle)

获取与 fileHandle关联的文件或目录的指定 FileAttributes

GetAttributes(String)

获取在此路径上的文件的 FileAttributes

GetAttributes(SafeFileHandle)

Source:
File.cs
Source:
File.cs
Source:
File.cs

获取与 fileHandle关联的文件或目录的指定 FileAttributes

public:
 static System::IO::FileAttributes GetAttributes(Microsoft::Win32::SafeHandles::SafeFileHandle ^ fileHandle);
public static System.IO.FileAttributes GetAttributes (Microsoft.Win32.SafeHandles.SafeFileHandle fileHandle);
static member GetAttributes : Microsoft.Win32.SafeHandles.SafeFileHandle -> System.IO.FileAttributes
Public Shared Function GetAttributes (fileHandle As SafeFileHandle) As FileAttributes

参数

fileHandle
SafeFileHandle

SafeFileHandle 检索其属性的文件或目录的 。

返回

FileAttributes文件或目录的 。

例外

fileHandlenull

调用方没有所要求的权限。

适用于

GetAttributes(String)

Source:
File.cs
Source:
File.cs
Source:
File.cs

获取在此路径上的文件的 FileAttributes

public:
 static System::IO::FileAttributes GetAttributes(System::String ^ path);
public static System.IO.FileAttributes GetAttributes (string path);
static member GetAttributes : string -> System.IO.FileAttributes
Public Shared Function GetAttributes (path As String) As FileAttributes

参数

path
String

文件的路径。

返回

路径上文件的 FileAttributes

例外

.NET Framework和 .NET Core 版本早于 2.1:path为空、仅包含空格或包含无效字符。

指定的路径和/或文件名超过了系统定义的最大长度。

path 的格式无效。

path 表示一个文件且它是无效的,例如,位于未映射的驱动器上或找不到该文件。

path 表示一个目录且它是无效的,例如,位于未映射的驱动器上或找不到该目录。

此文件正由另一个进程使用。

调用方没有所要求的权限。

示例

以下示例通过将 和 SetAttributes 属性应用于Archive文件来演示 GetAttributesHidden 方法。

using namespace System;
using namespace System::IO;
using namespace System::Text;
int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   
   // Create the file if it does not exist.
   if (  !File::Exists( path ) )
   {
      File::Create( path );
   }

   if ( (File::GetAttributes( path ) & FileAttributes::Hidden) == FileAttributes::Hidden )
   {
      
      // Show the file.
      File::SetAttributes(path, File::GetAttributes( path ) & ~FileAttributes::Hidden);
      Console::WriteLine( "The {0} file is no longer hidden.", path );
   }
   else
   {
      
      // Hide the file.
      File::SetAttributes( path, static_cast<FileAttributes>(File::GetAttributes( path ) | FileAttributes::Hidden) );
      Console::WriteLine( "The {0} file is now hidden.", path );
   }
}
using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // Create the file if it does not exist.
        if (!File.Exists(path))
        {
            File.Create(path);
        }

        FileAttributes attributes = File.GetAttributes(path);

        if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
        {
            // Show the file.
            attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
            File.SetAttributes(path, attributes);
            Console.WriteLine("The {0} file is no longer hidden.", path);
        }
        else
        {
            // Hide the file.
            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;
    }
}
open System.IO
open System.Text

let removeAttribute attributes attributesToRemove = attributes &&& ~~~attributesToRemove

let path = @"c:\temp\MyTest.txt"

// Create the file if it does not exist.
if File.Exists path |> not then
    File.Create path |> ignore

let attributes = File.GetAttributes path

if attributes &&& FileAttributes.Hidden = FileAttributes.Hidden then
    // Show the file.
    let attributes =
        removeAttribute attributes FileAttributes.Hidden

    File.SetAttributes(path, attributes)
    printfn $"The {path} file is no longer hidden."
else
    // Hide the file.
    File.SetAttributes(path, File.GetAttributes path ||| FileAttributes.Hidden)
    printfn $"The {path} file is now hidden."
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"

        ' Create the file if it does not exist.
        If File.Exists(path) = False Then
            File.Create(path)
        End If

        Dim attributes As FileAttributes
        attributes = File.GetAttributes(path)

        If (attributes And FileAttributes.Hidden) = FileAttributes.Hidden Then
            ' Show the file.
            attributes = RemoveAttribute(attributes, FileAttributes.Hidden)
            File.SetAttributes(path, attributes)
            Console.WriteLine("The {0} file is no longer hidden.", path)
        Else
            ' Hide the file.
            File.SetAttributes(path, File.GetAttributes(path) Or FileAttributes.Hidden)
            Console.WriteLine("The {0} file is now hidden.", path)
        End If
    End Sub

    Public Shared Function RemoveAttribute(ByVal attributes As FileAttributes, ByVal attributesToRemove As FileAttributes) As FileAttributes
        Return attributes And (Not attributesToRemove)
    End Function
End Class

注解

允许 path 参数指定相对或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory

有关常见 I/O 任务的列表,请参阅 常见 I/O 任务

另请参阅

适用于