FileInfo 類別

定義

提供建立、複製、刪除、移動及開啟檔案的屬性與實例方法,並協助物件建立 FileStream 。 此類別無法獲得繼承。

public ref class FileInfo sealed : System::IO::FileSystemInfo
public sealed class FileInfo : System.IO.FileSystemInfo
[System.Serializable]
public sealed class FileInfo : System.IO.FileSystemInfo
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class FileInfo : System.IO.FileSystemInfo
type FileInfo = class
    inherit FileSystemInfo
[<System.Serializable>]
type FileInfo = class
    inherit FileSystemInfo
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type FileInfo = class
    inherit FileSystemInfo
Public NotInheritable Class FileInfo
Inherits FileSystemInfo
繼承
繼承
屬性

範例

以下範例展示了該 FileInfo 類別中的一些主要成員。

當屬性首次取得時,呼叫 FileInfoRefresh 方法並快取關於檔案的資訊。 在後續通話中,您必須致電 Refresh 取得最新資訊副本。

using System;
using System.IO;

class Test
{
    
    public static void Main()
    {
        string path = Path.GetTempFileName();
        var fi1 = new FileInfo(path);

        // Create a file to write to.
        using (StreamWriter sw = fi1.CreateText())
        {
            sw.WriteLine("Hello");
            sw.WriteLine("And");
            sw.WriteLine("Welcome");
        }	

        // Open the file to read from.
        using (StreamReader sr = fi1.OpenText())
        {
            var s = "";
            while ((s = sr.ReadLine()) != null)
            {
                Console.WriteLine(s);
            }
        }

        try
        {
            string path2 = Path.GetTempFileName();
            var fi2 = new FileInfo(path2);

            // Ensure that the target does not exist.
            fi2.Delete();

            // Copy the file.
            fi1.CopyTo(path2);
            Console.WriteLine($"{path} was copied to {path2}.");

            // Delete the newly created file.
            fi2.Delete();
            Console.WriteLine($"{path2} was successfully deleted.");
        }
        catch (Exception e)
        {
            Console.WriteLine($"The process failed: {e.ToString()}");
        }
    }
}
Imports System.IO

Public Class Test

    Public Shared Sub Main()
        Dim path1 As String = Path.GetTempFileName()
        Dim path2 As String = Path.GetTempFileName()
        Dim fi As New FileInfo(path1)

        ' Create a file to write to.
        Using sw As StreamWriter = fi.CreateText()
            sw.WriteLine("Hello")
            sw.WriteLine("And")
            sw.WriteLine("Welcome")
        End Using

        Try
            ' Open the file to read from.
            Using sr As StreamReader = fi.OpenText()
                Do While sr.Peek() >= 0
                    Console.WriteLine(sr.ReadLine())
                Loop
            End Using

            Dim fi2 As New FileInfo(path2)

            ' Ensure that the target does not exist.
            fi2.Delete()

            ' Copy the file.
            fi.CopyTo(path2)
            Console.WriteLine($"{path1} was copied to {path2}.")

            ' Delete the newly created file.
            fi2.Delete()
            Console.WriteLine($"{path2} was successfully deleted.")

        Catch e As Exception
            Console.WriteLine($"The process failed: {e.ToString()}.")
        End Try
    End Sub
End Class

此範例會產生類似下列的輸出。

Hello
And
Welcome
C:\Users\userName\AppData\Local\Temp\tmp70AB.tmp was copied to C:\Users\userName\AppData\Local\Temp\tmp70CB.tmp.
C:\Users\userName\AppData\Local\Temp\tmp70CB.tmp was successfully deleted.

備註

使用這個 FileInfo 類別來執行典型操作,例如複製、移動、重新命名、建立、開啟、刪除及附加檔案。

如果你對同一檔案執行多個操作,使用 FileInfo 實例方法而非類別對應的靜態方法 File 更有效率,因為安全性檢查不一定是必要的。

許多方法在建立或開啟檔案時會 FileInfo 回傳其他 I/O 類型。 您可以使用這些其他類型的來進一步操作檔案。 欲了解更多資訊,請參閱具體FileInfo成員如 OpenOpenReadOpenTextCreateTextCreate、 或 。

根據預設,對新檔案的完整讀取/寫入存取權會授與所有使用者。

下表描述用於客製化各種 FileInfo 方法行為的列舉。

Enumeration 描述
FileAccess 指定檔案的讀取和寫入存取權。
FileShare 指定已使用之檔案允許的存取層級。
FileMode 指定是否保留或覆寫現有檔案的內容,以及建立現有檔案的要求是否造成例外狀況。

Note

在接受路徑做為輸入字串的成員中,該路徑的格式必須良好或引發例外狀況。 例如,如果路徑為完整,但開頭為空格,則路徑不會在 類別的方法中修剪。 因此,路徑的格式不正確,而且會引發例外狀況。 同樣地,路徑或路徑的組合不能完全限定兩次。 例如,在大部分情況下,“c:\temp c:\windows” 也會引發例外狀況。 使用接受路徑字串的方法時,請確定您的路徑格式良好。

在接受路徑的成員中,路徑可以參考檔案或只參考目錄。 指定的路徑也可以參考伺服器和共享名稱的相對路徑或通用命名慣例 (UNC) 路徑。 例如,下列所有都是可接受的路徑:

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

  • C# 中的 “c:\\MyDir” 或 Visual Basic 中的 “c:\MyDir”。

  • C# 中的 “MyDir\\MySubdir” 或 Visual Basic 中的 “MyDir\MySubDir”。

  • C# 中的 “\\\\MyServer\\MyShare” 或 Visual Basic 中的 “\\MyServer\MyShare”。

這個 FileInfo 類別提供了以下屬性,讓你能夠取得關於檔案的資訊。 如需如何使用每個屬性的範例,請參閱屬性頁。

  • 這個 Directory 屬性會取得一個物件,代表檔案的父目錄。

  • DirectoryName 屬性可取得檔案父目錄的完整路徑。

  • Exists 屬性會在操作前檢查檔案的存在。

  • 這個 IsReadOnly 屬性會擷取或設定一個值,指定檔案是否可以被修改。

  • Length 會取得檔案大小。

  • Name 會取得檔案名稱。

建構函式

名稱 Description
FileInfo(String)

初始化一個新的類別實例 FileInfo ,作為檔案路徑的包裝器。

欄位

名稱 Description
FullPath

表示目錄或檔案的完整路徑。

(繼承來源 FileSystemInfo)
OriginalPath

使用者原本指定的路徑,無論是相對路徑還是絕對路徑。

(繼承來源 FileSystemInfo)

屬性

名稱 Description
Attributes

取得或設定目前檔案或目錄的屬性。

(繼承來源 FileSystemInfo)
CreationTime

取得或設定目前檔案或目錄的建立時間。

(繼承來源 FileSystemInfo)
CreationTimeUtc

取得或設定目前檔案或目錄的建立時間,以協調的通用時間(UTC)為基準。

(繼承來源 FileSystemInfo)
Directory

會取得父目錄的實例。

DirectoryName

會取得一個字串,代表該目錄的完整路徑。

Exists

取得值,指出檔案是否存在。

Extension

取得檔名的副檔名部分,包括開頭的點 . ,即使它是整個檔名;若沒有副檔名則是空字串。

(繼承來源 FileSystemInfo)
FullName

取得目錄或檔案的完整路徑。

(繼承來源 FileSystemInfo)
IsReadOnly

取得或設定一個值,用以判斷目前檔案是否為唯讀。

LastAccessTime

取得或設定目前檔案或目錄最後被存取的時間。

(繼承來源 FileSystemInfo)
LastAccessTimeUtc

取得或設定協調世界時間(UTC)中,目前檔案或目錄最後被存取的時間。

(繼承來源 FileSystemInfo)
LastWriteTime

取得或設定目前檔案或目錄最後一次寫入的時間。

(繼承來源 FileSystemInfo)
LastWriteTimeUtc

取得或設定協調世界時間(UTC)中當前檔案或目錄最後一次寫入的時間。

(繼承來源 FileSystemInfo)
Length

取得目前檔案的大小,以位元組為單位。

Name

取得檔案的名稱。

方法

名稱 Description
AppendText()

建立 StreamWriter 一個,將文字附加到由此實例 FileInfo所代表的檔案中。

CopyTo(String, Boolean)

將現有的檔案複製到新的檔案,允許覆寫現有的檔案。

CopyTo(String)

將現有的檔案複製到新的檔案,不允許覆寫現有的檔案。

Create()

建立檔案。

CreateObjRef(Type)

建立一個物件,包含產生代理伺服器所需的所有相關資訊,用於與遠端物件通訊。

(繼承來源 MarshalByRefObject)
CreateText()

建立 StreamWriter 一個會寫入新的文字檔。

Decrypt()

解密由活期帳戶用此 Encrypt() 方法加密的檔案。

Delete()

永久刪除檔案。

Encrypt()

加密檔案,讓只有用來加密檔案的帳戶才能解密它。

Equals(Object)

判斷指定的 物件是否等於目前的物件。

(繼承來源 Object)
GetAccessControl()

取得 FileSecurity 一個封裝該檔案 FileInfo 存取控制清單(ACL)條目的物件。

GetAccessControl(AccessControlSections)

取得 FileSecurity 一個封裝了當前物件所描述 FileInfo 檔案指定類型的存取控制清單(ACL)條目的物件。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()

取得目前控制此實例生命週期政策的終身服務物件。

(繼承來源 MarshalByRefObject)
GetObjectData(SerializationInfo, StreamingContext)

以檔案名稱及額外例外資訊設定物件。SerializationInfo

(繼承來源 FileSystemInfo)
GetType()

取得目前實例的 Type

(繼承來源 Object)
InitializeLifetimeService()

取得一個終身服務物件以控制此實例的終身政策。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立一個 MarshalByRefObject 目前物件的淺層複製品。

(繼承來源 MarshalByRefObject)
MoveTo(String)

將指定的檔案移至新位置,並提供指定新檔名的選項。

Open(FileMode, FileAccess, FileShare)

以指定模式開啟具有讀取、寫入或讀取/寫入存取權和指定共用選項的檔案。

Open(FileMode, FileAccess)

以指定的模式開啟具有讀取、寫入或讀取/寫入存取權的檔案。

Open(FileMode)

以指定的模式開啟檔案。

OpenRead()

建立一個唯讀 FileStream的 。

OpenText()

建立一個帶有 UTF8 編碼的程式碼 StreamReader ,從現有的文字檔讀取。

OpenWrite()

建立一個唯寫 FileStream

Refresh()

重新整理 物件的狀態。

(繼承來源 FileSystemInfo)
Replace(String, String, Boolean)

將指定檔案的內容替換為目前 FileInfo 物件描述的檔案,刪除原始檔案,並備份被替換的檔案。 也指定是否忽略合併錯誤。

Replace(String, String)

將指定檔案的內容替換為目前 FileInfo 物件描述的檔案,刪除原始檔案並備份被替換的檔案。

SetAccessControl(FileSecurity)

將物件描述 FileSecurity 的存取控制清單(ACL)條目套用到目前 FileInfo 物件所描述的檔案上。

ToString()

回傳傳給 FileInfo 建構子的原始路徑。 完整路徑或檔名使用 FullName or Name 屬性。

適用於

另請參閱