FileSystemInfo FileSystemInfo FileSystemInfo FileSystemInfo Class

Definizione

Fornisce la classe di base per gli oggetti FileInfo e DirectoryInfo.Provides the base class for both FileInfo and DirectoryInfo objects.

public ref class FileSystemInfo abstract : MarshalByRefObject, System::Runtime::Serialization::ISerializable
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public abstract class FileSystemInfo : MarshalByRefObject, System.Runtime.Serialization.ISerializable
type FileSystemInfo = class
    inherit MarshalByRefObject
    interface ISerializable
Public MustInherit Class FileSystemInfo
Inherits MarshalByRefObject
Implements ISerializable
Ereditarietà
Derivato
Attributi
Implementazioni

Esempi

Nell'esempio seguente viene illustrato come eseguire il ciclo di tutti i file e le directory, eseguendo una query su alcune informazioni su ogni voce.The following example shows how to loop through all the files and directories, querying some information about each entry.

using namespace System;
using namespace System::IO;


namespace ConsoleApplication2
{
    public ref class Program
    {
    public:
        static void Main()
        {
            //  Loop through all the immediate subdirectories of C.
            for each (String^ entry in Directory::GetDirectories("C:\\"))
            {
                DisplayFileSystemInfoAttributes(gcnew DirectoryInfo(entry));
            }
            //  Loop through all the files in C.
            for each (String^ entry in Directory::GetFiles("C:\\"))
            {
                DisplayFileSystemInfoAttributes(gcnew FileInfo(entry));
            }
        }
        static void DisplayFileSystemInfoAttributes(FileSystemInfo^ fsi)
        {
            //  Assume that this entry is a file.
            String^ entryType = "File";

            // Determine if entry is really a directory
            if ((fsi->Attributes & FileAttributes::Directory) == FileAttributes::Directory)
            {
                entryType = "Directory";
            }
            //  Show this entry's type, name, and creation date.
            Console::WriteLine("{0} entry {1} was created on {2:D}", entryType, fsi->FullName, fsi->CreationTime);
        }
    };
};

int main()
{
    ConsoleApplication2::Program::Main();
}

 // Output will vary based on contents of drive C.

 // Directory entry C:\Documents and Settings was created on Tuesday, November 25, 2003
 // Directory entry C:\Inetpub was created on Monday, January 12, 2004
 // Directory entry C:\Program Files was created on Tuesday, November 25, 2003
 // Directory entry C:\RECYCLER was created on Tuesday, November 25, 2003
 // Directory entry C:\System Volume Information was created on Tuesday, November 2, 2003
 // Directory entry C:\WINDOWS was created on Tuesday, November 25, 2003
 // File entry C:\IO.SYS was created on Tuesday, November 25, 2003
 // File entry C:\MSDOS.SYS was created on Tuesday, November 25, 2003
 // File entry C:\pagefile.sys was created on Saturday, December 27, 2003
using System;
using System.IO;


namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            //  Loop through all the immediate subdirectories of C.
            foreach (string entry in Directory.GetDirectories(@"C:\"))
            {
                DisplayFileSystemInfoAttributes(new DirectoryInfo(entry));
            }
            //  Loop through all the files in C.
            foreach (string entry in Directory.GetFiles(@"C:\"))
            {
                DisplayFileSystemInfoAttributes(new FileInfo(entry));
            }
        }
        static void DisplayFileSystemInfoAttributes(FileSystemInfo fsi)
        {
            //  Assume that this entry is a file.
            string entryType = "File";

            // Determine if entry is really a directory
            if ((fsi.Attributes & FileAttributes.Directory) == FileAttributes.Directory )
            {
                entryType = "Directory";
            }
            //  Show this entry's type, name, and creation date.
            Console.WriteLine("{0} entry {1} was created on {2:D}", entryType, fsi.FullName, fsi.CreationTime);
        }
    }
}

 // Output will vary based on contents of drive C.
 
 // Directory entry C:\Documents and Settings was created on Tuesday, November 25, 2003
 // Directory entry C:\Inetpub was created on Monday, January 12, 2004
 // Directory entry C:\Program Files was created on Tuesday, November 25, 2003
 // Directory entry C:\RECYCLER was created on Tuesday, November 25, 2003
 // Directory entry C:\System Volume Information was created on Tuesday, November 2, 2003
 // Directory entry C:\WINDOWS was created on Tuesday, November 25, 2003
 // File entry C:\IO.SYS was created on Tuesday, November 25, 2003
 // File entry C:\MSDOS.SYS was created on Tuesday, November 25, 2003
 // File entry C:\pagefile.sys was created on Saturday, December 27, 2003
Imports System.IO
Module Module1

    Sub Main()
        ' Loop through all the immediate subdirectories of C.
        For Each entry As String In Directory.GetDirectories("C:\")
            DisplayFileSystemInfoAttributes(New DirectoryInfo(entry))
        Next

        ' Loop through all the files in C.
        For Each entry As String In Directory.GetFiles("C:\")
            DisplayFileSystemInfoAttributes(New FileInfo(entry))
        Next
    End Sub

    Sub DisplayFileSystemInfoAttributes(ByVal fsi As IO.FileSystemInfo)
        ' Assume that this entry is a file.
        Dim entryType As String = "File"

        ' Determine if this entry is really a directory.
        If (fsi.Attributes And FileAttributes.Directory) = FileAttributes.Directory Then
            entryType = "Directory"
        End If

        ' Show this entry's type, name, and creation date.
        Console.WriteLine("{0} entry {1} was created on {2:D}", _
            entryType, fsi.FullName, fsi.CreationTime)
    End Sub
End Module

' Output will vary based on contents of drive C.
' 
' Directory entry C:\Documents and Settings was created on Tuesday, November 25, 2003
' Directory entry C:\Inetpub was created on Monday, January 12, 2004
' Directory entry C:\Program Files was created on Tuesday, November 25, 2003
' Directory entry C:\RECYCLER was created on Tuesday, November 25, 2003
' Directory entry C:\System Volume Information was created on Tuesday, November 2, 2003
' Directory entry C:\WINDOWS was created on Tuesday, November 25, 2003
' File entry C:\IO.SYS was created on Tuesday, November 25, 2003
' File entry C:\MSDOS.SYS was created on Tuesday, November 25, 2003
' File entry C:\pagefile.sys was created on Saturday, December 27, 2003

Commenti

La FileSystemInfo classe contiene metodi comuni alla manipolazione di file e directory.The FileSystemInfo class contains methods that are common to file and directory manipulation. Un FileSystemInfo oggetto può rappresentare un file o una directory, in modo da fungere da base FileInfo per DirectoryInfo gli oggetti o.A FileSystemInfo object can represent either a file or a directory, thus serving as the basis for FileInfo or DirectoryInfo objects. Usare questa classe di base per l'analisi di una grande quantità di file e directory.Use this base class when parsing a lot of files and directories.

Una classe derivata può ereditare FileSystemInfo da solo se la classe derivata ha l'autorizzazione AllAccess FileIOPermissionAccess dall'enumerazione.A derived class can inherit from FileSystemInfo only if the derived class has the AllAccess permission from the FileIOPermissionAccess enumeration.

Nei membri che accettano un percorso, il percorso può fare riferimento a un file o solo a una directory.In members that accept a path, the path can refer to a file or just a directory. Il percorso specificato può anche fare riferimento a un percorso relativo o a un percorso di Universal Naming Convention (UNC) per un server e un nome di condivisione.The specified path can also refer to a relative path or a Universal Naming Convention (UNC) path for a server and share name. Ad esempio, tutti i percorsi seguenti sono accettabili:For example, all the following are acceptable paths:

  • "c:\\mydir\\MyFile.txt" in C#o "c:\mydir\myfile.txt" in Visual Basic."c:\\MyDir\\MyFile.txt" in C#, or "c:\MyDir\MyFile.txt" in Visual Basic.

  • "c:\\MyDir" in C#o "c:\MyDir" in Visual Basic."c:\\MyDir" in C#, or "c:\MyDir" in Visual Basic.

  • "Mydir\\MySubdir" in C#o "MyDir\MySubDir" in Visual Basic."MyDir\\MySubdir" in C#, or "MyDir\MySubDir" in Visual Basic.

  • "\\ C#\MyServer \MyShare" in o"\\MyServer\MyShare" in Visual Basic.\\"\\\\MyServer\\MyShare" in C#, or "\\MyServer\MyShare" in Visual Basic.

Per un elenco delle attività di I/O comuni, vedere attività di i/o comuni.For a list of common I/O tasks, see Common I/O Tasks.

Costruttori

FileSystemInfo() FileSystemInfo() FileSystemInfo() FileSystemInfo()

Inizializza una nuova istanza della classe FileSystemInfo.Initializes a new instance of the FileSystemInfo class.

FileSystemInfo(SerializationInfo, StreamingContext) FileSystemInfo(SerializationInfo, StreamingContext) FileSystemInfo(SerializationInfo, StreamingContext) FileSystemInfo(SerializationInfo, StreamingContext)

Inizializza una nuova istanza della classe FileSystemInfo con dati serializzati.Initializes a new instance of the FileSystemInfo class with serialized data.

Campi

FullPath FullPath FullPath FullPath

Rappresenta il percorso completo del file o della directory.Represents the fully qualified path of the directory or file.

OriginalPath OriginalPath OriginalPath OriginalPath

Percorso, assoluto o relativo, originariamente specificato dall'utente.The path originally specified by the user, whether relative or absolute.

Proprietà

Attributes Attributes Attributes Attributes

Ottiene o imposta gli attributi per il file o la directory corrente.Gets or sets the attributes for the current file or directory.

CreationTime CreationTime CreationTime CreationTime

Ottiene o imposta l'ora di creazione del file o della directory corrente.Gets or sets the creation time of the current file or directory.

CreationTimeUtc CreationTimeUtc CreationTimeUtc CreationTimeUtc

Ottiene o imposta l'ora di creazione, nell'ora UTC (Coordinated Universal Time) del file o della directory corrente.Gets or sets the creation time, in coordinated universal time (UTC), of the current file or directory.

Exists Exists Exists Exists

Recupera un valore che indica se il file o la directory esiste.Gets a value indicating whether the file or directory exists.

Extension Extension Extension Extension

Recupera la stringa che rappresenta l'estensione del file.Gets the string representing the extension part of the file.

FullName FullName FullName FullName

Recupera il percorso completo del file o della directory.Gets the full path of the directory or file.

LastAccessTime LastAccessTime LastAccessTime LastAccessTime

Recupera o imposta l'ora in cui è avvenuto l'ultimo accesso al file o alla directory corrente.Gets or sets the time the current file or directory was last accessed.

LastAccessTimeUtc LastAccessTimeUtc LastAccessTimeUtc LastAccessTimeUtc

Ottiene o imposta l'ora, nell'ora UTC (Universal Coordinated Time), dell'ultimo accesso al file o alla directory corrente.Gets or sets the time, in coordinated universal time (UTC), that the current file or directory was last accessed.

LastWriteTime LastWriteTime LastWriteTime LastWriteTime

Recupera o imposta l'ora dell'ultima modifica del file o della directory corrente.Gets or sets the time when the current file or directory was last written to.

LastWriteTimeUtc LastWriteTimeUtc LastWriteTimeUtc LastWriteTimeUtc

Ottiene o imposta l'ora, nell'ora UTC (Universal Coordinated Time), dell'ultima scrittura al file o alla directory corrente.Gets or sets the time, in coordinated universal time (UTC), when the current file or directory was last written to.

Name Name Name Name

Nel caso di file, ottiene il nome del file.For files, gets the name of the file. Nel caso di directory, ottiene il nome dell'ultima directory nella gerarchia, se esiste una gerarchia.For directories, gets the name of the last directory in the hierarchy if a hierarchy exists. In caso contrario, la proprietà Name ottiene il nome della directory.Otherwise, the Name property gets the name of the directory.

Metodi

CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type)

Consente di creare un oggetto che contiene tutte le informazioni rilevanti necessarie per la generazione del proxy utilizzato per effettuare la comunicazione con un oggetto remoto.Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
Delete() Delete() Delete() Delete()

Elimina un file o una directory.Deletes a file or directory.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Funge da funzione hash predefinita.Serves as the default hash function.

(Inherited from Object)
GetLifetimeService() GetLifetimeService() GetLifetimeService() GetLifetimeService()

Consente di recuperare l'oggetto servizio di durata corrente per controllare i criteri di durata per l'istanza.Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext)

Imposta l'oggetto SerializationInfo con il nome del file e le informazioni aggiuntive sull'eccezione.Sets the SerializationInfo object with the file name and additional exception information.

GetType() GetType() GetType() GetType()

Ottiene l'oggetto Type dell'istanza corrente.Gets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService()

Ottiene un oggetto servizio di durata per controllare i criteri di durata per questa istanza.Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean)

Crea una copia dei riferimenti dell'oggetto MarshalByRefObject corrente.Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
Refresh() Refresh() Refresh() Refresh()

Aggiorna lo stato dell'oggetto.Refreshes the state of the object.

ToString() ToString() ToString() ToString()

Restituisce il percorso originale.Returns the original path. Usare le proprietà FullName o Name per il percorso completo o il nome di file/directory.Use the FullName or Name properties for the full path or file/directory name.

Sicurezza

FileIOPermission
per l'autorizzazione a ereditare da questa classe.for permission to inherit from this class. Enumerazione associata:UnrestrictedAssociated enumeration: Unrestricted Azione di sicurezza: Richiesta di ereditarietàSecurity Action: Inheritance Demand

Si applica a

Vedi anche