File.GetAttributes Méthode

Définition

Surcharges

GetAttributes(SafeFileHandle)

Obtient le spécifié FileAttributes du fichier ou du répertoire associé fileHandleà .

GetAttributes(String)

Obtient l'élément FileAttributes du fichier sur le chemin d'accès.

GetAttributes(SafeFileHandle)

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

Obtient le spécifié FileAttributes du fichier ou du répertoire associé fileHandleà .

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

Paramètres

fileHandle
SafeFileHandle

SafeFileHandle dans le fichier ou le répertoire pour lequel les attributs doivent être récupérés.

Retours

du FileAttributes fichier ou du répertoire.

Exceptions

fileHandle a la valeur null.

L'appelant n'a pas l'autorisation requise.

S’applique à

GetAttributes(String)

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

Obtient l'élément FileAttributes du fichier sur le chemin d'accès.

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

Paramètres

path
String

Chemin d'accès au fichier.

Retours

Élément FileAttributes du fichier sur le chemin d'accès.

Exceptions

.NET Framework et .NET Core versions antérieures à 2.1 : path est vide, contient uniquement des espaces blancs ou contient des caractères non valides.

Le chemin et/ou le nom de fichier spécifiés dépassent la longueur maximale définie par le système.

path est dans un format non valide.

path représente un fichier et n’est pas valide, comme sur un lecteur non mappé, ou le fichier est introuvable.

path représente un répertoire et n’est pas valide, comme sur un lecteur non mappé, ou le répertoire est introuvable.

Le fichier est utilisé par un autre processus.

L'appelant n'a pas l'autorisation requise.

Exemples

L’exemple suivant illustre les GetAttributes méthodes et SetAttributes en appliquant les Archive attributs et Hidden à un fichier.

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

Remarques

Le path paramètre est autorisé à spécifier des informations relatives ou absolues sur le chemin d’accès. Les informations relatives au chemin d’accès sont interprétées comme relatives au répertoire de travail actuel. Pour obtenir le répertoire de travail actuel, consultez GetCurrentDirectory.

Pour obtenir la liste des tâches d’E/S courantes, consultez Tâches courantes d’E/S.

Voir aussi

S’applique à