FileSystemInfo FileSystemInfo FileSystemInfo FileSystemInfo Class

定義

FileInfoDirectoryInfo 物件提供基底類別。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
繼承
衍生
屬性
實作

範例

下列範例示範如何執行迴圈的所有檔案和目錄,查詢每個項目的一些資訊。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

備註

FileSystemInfo類別含有通用於檔案和目錄操作的方法。The FileSystemInfo class contains methods that are common to file and directory manipulation. AFileSystemInfo物件可以代表檔案或目錄,做為基礎,因此FileInfoDirectoryInfo物件。A FileSystemInfo object can represent either a file or a directory, thus serving as the basis for FileInfo or DirectoryInfo objects. 剖析許多檔案和目錄時,請使用這個基底類別。Use this base class when parsing a lot of files and directories.

在衍生的類別可以繼承自FileSystemInfo衍生的類別有 AllAccess 權限時,才FileIOPermissionAccess列舉型別。A derived class can inherit from FileSystemInfo only if the derived class has the AllAccess permission from the FileIOPermissionAccess enumeration.

在接受路徑的成員,路徑可以參考到檔案或只是一個目錄。In members that accept a path, the path can refer to a file or just a directory. 指定的路徑也可以參考相對路徑或通用命名慣例 (UNC) 路徑的伺服器和共用的名稱。The specified path can also refer to a relative path or a Universal Naming Convention (UNC) path for a server and share name. 例如,如果以下所有狀況可接受的路徑:For example, all the following are acceptable paths:

  • "c:\\MyDir\\MyFile.txt 」 以 C# 或 Visual Basic 中的 「 c:\MyDir\MyFile.txt"。"c:\\MyDir\\MyFile.txt" in C#, or "c:\MyDir\MyFile.txt" in Visual Basic.

  • "c:\\MyDir 」 以 C# 或 Visual Basic 中的 「 c:\mydir"。"c:\\MyDir" in C#, or "c:\MyDir" in Visual Basic.

  • "MyDir\\MySubdir 」 以 C# 或 Visual Basic 中的 「 MyDir\MySubDir"。"MyDir\\MySubdir" in C#, or "MyDir\MySubDir" in Visual Basic.

  • 「\\\\MyServer\\MyShare 」 在 C# 中,或 「\\MyServer\MyShare"在 Visual Basic 中。"\\\\MyServer\\MyShare" in C#, or "\\MyServer\MyShare" in Visual Basic.

如需一般 I/O 工作的清單,請參閱 < 一般 I/O 工作For a list of common I/O tasks, see Common I/O Tasks.

建構函式

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

初始化 FileSystemInfo 類別的新執行個體。Initializes a new instance of the FileSystemInfo class.

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

使用序列化資料,初始化 FileSystemInfo 類別的新執行個體。Initializes a new instance of the FileSystemInfo class with serialized data.

欄位

FullPath FullPath FullPath FullPath

代表目錄或檔案的完整路徑。Represents the fully qualified path of the directory or file.

OriginalPath OriginalPath OriginalPath OriginalPath

使用者指定的相對或絕對的原始路徑。The path originally specified by the user, whether relative or absolute.

屬性

Attributes Attributes Attributes Attributes

取得或設定目前檔案或目錄的屬性。Gets or sets the attributes for the current file or directory.

CreationTime CreationTime CreationTime CreationTime

取得或設定目前檔案或目錄的建立時間。Gets or sets the creation time of the current file or directory.

CreationTimeUtc CreationTimeUtc CreationTimeUtc CreationTimeUtc

取得或設定目前檔案或目錄的建立時間,其格式為國際標準時間 (Coordinated Universal Time,UTC)。Gets or sets the creation time, in coordinated universal time (UTC), of the current file or directory.

Exists Exists Exists Exists

取得值,表示檔案或目錄是否存在。Gets a value indicating whether the file or directory exists.

Extension Extension Extension Extension

取得字串,代表檔案的副檔名部分。Gets the string representing the extension part of the file.

FullName FullName FullName FullName

取得目錄或檔案的完整路徑。Gets the full path of the directory or file.

LastAccessTime LastAccessTime LastAccessTime LastAccessTime

取得或設定上次存取目前檔案或目錄的時間。Gets or sets the time the current file or directory was last accessed.

LastAccessTimeUtc LastAccessTimeUtc LastAccessTimeUtc LastAccessTimeUtc

取得或設定目前檔案或目錄上次被存取的時間,其格式為 Coordinated Universal Time (UTC)。Gets or sets the time, in coordinated universal time (UTC), that the current file or directory was last accessed.

LastWriteTime LastWriteTime LastWriteTime LastWriteTime

取得或設定上次寫入目前檔案或目錄的時間。Gets or sets the time when the current file or directory was last written to.

LastWriteTimeUtc LastWriteTimeUtc LastWriteTimeUtc LastWriteTimeUtc

取得或設定目前檔案或目錄上次被寫入的時間,其格式為 Coordinated Universal Time (UTC)。Gets or sets the time, in coordinated universal time (UTC), when the current file or directory was last written to.

Name Name Name Name

對於檔案,取得檔案的名稱。For files, gets the name of the file. 對於目錄,如果有階層架構,則是取得階層架構中最後一個目錄的名稱。For directories, gets the name of the last directory in the hierarchy if a hierarchy exists. 否則,Name 屬性會取得目錄的名稱。Otherwise, the Name property gets the name of the directory.

方法

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

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。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()

刪除檔案或目錄。Deletes a file or directory.

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

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

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

做為預設雜湊函式。Serves as the default hash function.

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

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。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)

以檔案名稱和其他例外狀況資訊設定 SerializationInfo 物件。Sets the SerializationInfo object with the file name and additional exception information.

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

取得目前執行個體的 TypeGets the Type of the current instance.

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

取得存留期服務物件,以控制這個執行個體的存留期原則。Obtains a lifetime service object to control the lifetime policy for this instance.

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

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

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

建立目前 MarshalByRefObject 物件的淺層複本。Creates a shallow copy of the current MarshalByRefObject object.

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

重新整理物件的狀態。Refreshes the state of the object.

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

傳回的原始路徑。Returns the original path. 使用FullNameName屬性的完整路徑或檔案/目錄名稱。Use the FullName or Name properties for the full path or file/directory name.

安全性

FileIOPermission
要從此類別繼承的權限。for permission to inherit from this class. 相關聯的列舉型別: UnrestrictedAssociated enumeration: Unrestricted 安全性動作:繼承要求Security Action: Inheritance Demand

適用於

另請參閱