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
下列範例示範
FileInfo
類別的一些主要成員。
第一次擷取屬性時,
FileInfo
會呼叫
Refresh
方法,並快取檔案的相關信息。 在後續的呼叫中,您必須呼叫
Refresh
,以取得最新的資訊複本。
using namespace System;
using namespace System::IO;
int main()
String^ path = Path::GetTempFileName();
FileInfo^ fi1 = gcnew FileInfo( path );
//Create a file to write to.
StreamWriter^ sw = fi1->CreateText();
sw->WriteLine( "Hello" );
sw->WriteLine( "And" );
sw->WriteLine( "Welcome" );
finally
if ( sw )
delete (IDisposable^)sw;
//Open the file to read from.
StreamReader^ sr = fi1->OpenText();
String^ s = "";
while ( s = sr->ReadLine() )
Console::WriteLine( s );
finally
if ( sr )
delete (IDisposable^)sr;
String^ path2 = Path::GetTempFileName();
FileInfo^ fi2 = gcnew FileInfo( path2 );
//Ensure that the target does not exist.
fi2->Delete();
//Copy the file.
fi1->CopyTo( path2 );
Console::WriteLine( "{0} was copied to {1}.", path, path2 );
//Delete the newly created file.
fi2->Delete();
Console::WriteLine( "{0} was successfully deleted.", path2 );
catch ( Exception^ e )
Console::WriteLine( "The process failed: {0}", e );
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);
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
' Open the file to read from.
Using sr As StreamReader = fi.OpenText()
Do While sr.Peek() >= 0
Console.WriteLine(sr.ReadLine())
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
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 成員,例如 Open、OpenRead、OpenText、CreateText或 Create。
根據預設,對新檔案的完整讀取/寫入存取權會授與所有使用者。
下表描述用來自定義各種 FileInfo 方法行為的列舉。
在接受路徑做為輸入字串的成員中,該路徑的格式必須良好或引發例外狀況。 例如,如果路徑為完整,但開頭為空格,則路徑不會在 類別的方法中修剪。 因此,路徑的格式不正確,而且會引發例外狀況。 同樣地,路徑或路徑的組合不能完全限定兩次。 例如,在大部分情況下,“c:\temp c:\windows” 也會引發例外狀況。 使用接受路徑字串的方法時,請確定您的路徑格式良好。
在接受路徑的成員中,路徑可以參考檔案或只參考目錄。 指定的路徑也可以參考伺服器和共享名稱的相對路徑或通用命名慣例 (UNC) 路徑。 例如,下列所有都是可接受的路徑:
“c:\\MyDir\\MyFile.txt”in C#, or “c:\MyDir\MyFile.txt” in Visual Basic.
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 會擷取檔名。