File.GetAttributes Methode

Definition

Überlädt

Name Beschreibung
GetAttributes(SafeFileHandle)

Ruft die angegebene FileAttributes der Datei oder des Verzeichnisses, die zugeordnet fileHandleist.

GetAttributes(String)

Ruft die FileAttributes Datei auf dem Pfad ab.

GetAttributes(SafeFileHandle)

Quelle:
File.cs
Quelle:
File.cs
Quelle:
File.cs
Quelle:
File.cs
Quelle:
File.cs

Ruft die angegebene FileAttributes der Datei oder des Verzeichnisses, die zugeordnet fileHandleist.

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

Parameter

fileHandle
SafeFileHandle

A SafeFileHandle für die Datei oder das Verzeichnis, für die die Attribute abgerufen werden sollen.

Gibt zurück

Die FileAttributes Datei oder das Verzeichnis.

Ausnahmen

fileHandle ist null.

Der Aufrufer verfügt nicht über die erforderliche Berechtigung.

Gilt für:

GetAttributes(String)

Quelle:
File.cs
Quelle:
File.cs
Quelle:
File.cs
Quelle:
File.cs
Quelle:
File.cs

Ruft die FileAttributes Datei auf dem Pfad ab.

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

Parameter

path
String

Der Pfad zur Datei.

Gibt zurück

Die FileAttributes Datei auf dem Pfad.

Ausnahmen

.NET Framework- und .NET Core-Versionen, die älter als 2.1 sind: path ist leer, enthält nur Leerzeichen oder ungültige Zeichen.

Der angegebene Pfad, der Dateiname oder beide überschreiten die vom System definierte maximale Länge.

path ist in einem ungültigen Format vorhanden.

path stellt eine Datei dar und ist ungültig, z. B. auf einem nicht zugeordneten Laufwerk, oder die Datei wurde nicht gefunden.

path stellt ein Verzeichnis dar und ist ungültig, z. B. auf einem nicht zugeordneten Laufwerk, oder das Verzeichnis kann nicht gefunden werden.

Diese Datei wird von einem anderen Prozess verwendet.

Der Aufrufer verfügt nicht über die erforderliche Berechtigung.

Beispiele

Das folgende Beispiel veranschaulicht die GetAttributes und SetAttributes methoden, indem die Archive Attribute Hidden auf eine Datei angewendet werden.

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

Hinweise

Der path Parameter darf relative oder absolute Pfadinformationen angeben. Relative Pfadinformationen werden relativ zum aktuellen Arbeitsverzeichnis interpretiert. Informationen zum Abrufen des aktuellen Arbeitsverzeichnisses finden Sie unter GetCurrentDirectory.

Eine Liste allgemeiner E/A-Aufgaben finden Sie unter "Allgemeine E/A-Aufgaben".

Weitere Informationen

Gilt für: