File.GetAttributes Metodo

Definizione

Overload

GetAttributes(SafeFileHandle)

Ottiene l'oggetto specificato FileAttributes del file o della directory associata a fileHandle.

GetAttributes(String)

Ottiene l'oggetto FileAttributes del file nel percorso.

GetAttributes(SafeFileHandle)

Origine:
File.cs
Origine:
File.cs
Origine:
File.cs

Ottiene l'oggetto specificato FileAttributes del file o della directory associata a 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

Parametri

fileHandle
SafeFileHandle

Oggetto SafeFileHandle nel file o nella directory per cui devono essere recuperati gli attributi.

Restituisce

Oggetto FileAttributes del file o della directory.

Eccezioni

fileHandle è null.

Il chiamante non dispone dell'autorizzazione richiesta.

Si applica a

GetAttributes(String)

Origine:
File.cs
Origine:
File.cs
Origine:
File.cs

Ottiene l'oggetto FileAttributes del file nel percorso.

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

Parametri

path
String

Percorso del file.

Restituisce

Oggetto FileAttributes del file nel percorso.

Eccezioni

.NET Framework e versioni di .NET Core precedenti alla 2.1: path è vuoto, contiene solo spazi vuoti o contiene caratteri non validi.

Il percorso specificato, il nome file o entrambi superano la lunghezza massima definita dal sistema.

Il formato di path non è valido.

path rappresenta un file e non è valido, ad esempio perché si trova in un'unità non connessa, oppure non è possibile trovare il file.

path rappresenta una directory e non è valido, ad esempio perché si trova in un'unità non connessa, oppure non è possibile trovare la directory.

Il file è usato da un altro processo.

Il chiamante non dispone dell'autorizzazione richiesta.

Esempio

Nell'esempio seguente vengono illustrati i GetAttributes metodi e SetAttributes applicando gli Archive attributi e Hidden a un file.

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

Commenti

Il path parametro è autorizzato a specificare informazioni relative o assolute sul percorso. Le informazioni relative sul percorso sono interpretate come relative alla directory di lavoro corrente. Per ottenere la directory di lavoro corrente, vedere GetCurrentDirectory.

Per un elenco delle attività di I/O comuni, vedere Attività di I/O comuni.

Vedi anche

Si applica a