DirectoryInfo 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
公開建立、移動和全面列舉目錄和子目錄的執行個體 (Instance) 方法。 此類別無法獲得繼承。
public ref class DirectoryInfo sealed : System::IO::FileSystemInfo
public sealed class DirectoryInfo : System.IO.FileSystemInfo
[System.Serializable]
public sealed class DirectoryInfo : System.IO.FileSystemInfo
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class DirectoryInfo : System.IO.FileSystemInfo
type DirectoryInfo = class
inherit FileSystemInfo
[<System.Serializable>]
type DirectoryInfo = class
inherit FileSystemInfo
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type DirectoryInfo = class
inherit FileSystemInfo
Public NotInheritable Class DirectoryInfo
Inherits FileSystemInfo
- 繼承
- 繼承
- 屬性
範例
下列範例示範 類別的 DirectoryInfo
一些主要成員。
using namespace System;
using namespace System::IO;
int main()
{
// Specify the directories you want to manipulate.
DirectoryInfo^ di = gcnew DirectoryInfo( "c:\\MyDir" );
try
{
// Determine whether the directory exists.
if ( di->Exists )
{
// Indicate that the directory already exists.
Console::WriteLine( "That path exists already." );
return 0;
}
// Try to create the directory.
di->Create();
Console::WriteLine( "The directory was created successfully." );
// Delete the directory.
di->Delete();
Console::WriteLine( "The directory was deleted successfully." );
}
catch ( Exception^ e )
{
Console::WriteLine( "The process failed: {0}", e );
}
}
using System;
using System.IO;
class Test
{
public static void Main()
{
// Specify the directories you want to manipulate.
DirectoryInfo di = new DirectoryInfo(@"c:\MyDir");
try
{
// Determine whether the directory exists.
if (di.Exists)
{
// Indicate that the directory already exists.
Console.WriteLine("That path exists already.");
return;
}
// Try to create the directory.
di.Create();
Console.WriteLine("The directory was created successfully.");
// Delete the directory.
di.Delete();
Console.WriteLine("The directory was deleted successfully.");
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
finally {}
}
}
open System.IO
// Specify the directories you want to manipulate.
let di = DirectoryInfo @"c:\MyDir"
try
// Determine whether the directory exists.
if di.Exists then
// Indicate that the directory already exists.
printfn "That path exists already."
else
// Try to create the directory.
di.Create()
printfn "The directory was created successfully."
// Delete the directory.
di.Delete()
printfn "The directory was deleted successfully."
with e ->
printfn $"The process failed: {e}"
Imports System.IO
Public Class Test
Public Shared Sub Main()
' Specify the directories you want to manipulate.
Dim di As DirectoryInfo = New DirectoryInfo("c:\MyDir")
Try
' Determine whether the directory exists.
If di.Exists Then
' Indicate that it already exists.
Console.WriteLine("That path exists already.")
Return
End If
' Try to create the directory.
di.Create()
Console.WriteLine("The directory was created successfully.")
' Delete the directory.
di.Delete()
Console.WriteLine("The directory was deleted successfully.")
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class
下列範例示範如何複製目錄及其內容。
using System;
using System.IO;
class CopyDir
{
public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
if (source.FullName.ToLower() == target.FullName.ToLower())
{
return;
}
// Check if the target directory exists, if not, create it.
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}
// Copy each file into it's new directory.
foreach (FileInfo fi in source.GetFiles())
{
Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
}
// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir =
target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}
public static void Main()
{
string sourceDirectory = @"c:\sourceDirectory";
string targetDirectory = @"c:\targetDirectory";
DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
CopyAll(diSource, diTarget);
}
// Output will vary based on the contents of the source directory.
}
open System.IO
let rec copyAll (source: DirectoryInfo) (target: DirectoryInfo) =
if source.FullName.ToLower() <> target.FullName.ToLower() then
// Check if the target directory exists, if not, create it.
if not (Directory.Exists target.FullName) then
Directory.CreateDirectory target.FullName |> ignore
// Copy each file into it's new directory.
for fi in source.GetFiles() do
printfn $@"Copying {target.FullName}\{fi.Name}"
fi.CopyTo(Path.Combine(string target, fi.Name), true) |> ignore
// Copy each subdirectory using recursion.
for diSourceSubDir in source.GetDirectories() do
target.CreateSubdirectory diSourceSubDir.Name
|> copyAll diSourceSubDir
let sourceDirectory = @"c:\sourceDirectory"
let targetDirectory = @"c:\targetDirectory"
let diSource = DirectoryInfo sourceDirectory
let diTarget = DirectoryInfo targetDirectory
copyAll diSource diTarget
// Output will vary based on the contents of the source directory.
Imports System.IO
Class CopyDir
Shared Sub CopyAll(ByVal source As DirectoryInfo, ByVal target As DirectoryInfo)
If (source.FullName.ToLower() = target.FullName.ToLower()) Then
Return
End If
' Check if the target directory exists, if not, create it.
If Directory.Exists(target.FullName) = False Then
Directory.CreateDirectory(target.FullName)
End If
' Copy each file into it's new directory.
For Each fi As FileInfo In source.GetFiles()
Console.WriteLine("Copying {0}\{1}", target.FullName, fi.Name)
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), True)
Next
' Copy each subdirectory using recursion.
For Each diSourceSubDir As DirectoryInfo In source.GetDirectories()
Dim nextTargetSubDir As DirectoryInfo = target.CreateSubdirectory(diSourceSubDir.Name)
CopyAll(diSourceSubDir, nextTargetSubDir)
Next
End Sub
Shared Sub Main()
Dim sourceDirectory As String = "c:\\sourceDirectory"
Dim targetDirectory As String = "c:\\targetDirectory"
Dim diSource As DirectoryInfo = New DirectoryInfo(sourceDirectory)
Dim diTarget As DirectoryInfo = New DirectoryInfo(targetDirectory)
CopyAll(diSource, diTarget)
End Sub
' Output will vary based on the contents of the source directory.
End Class
備註
使用 類別 DirectoryInfo 進行一般作業,例如複製、移動、重新命名、建立和刪除目錄。
如果您要重複使用物件數次,請考慮使用 的 DirectoryInfo 實例方法,而不是 類別的 Directory 對應靜態方法,因為安全性檢查不一定是必要的。
注意
在接受路徑做為輸入字串的成員中,該路徑的格式必須正確或引發例外狀況。 例如,如果路徑為完整,但開頭為空格,則路徑不會在 類別的方法中修剪。 因此,路徑格式不正確,並引發例外狀況。 同樣地,路徑或路徑組合不能完整兩次。 例如,在大部分情況下,“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”。
根據預設,會將新目錄的完整讀取/寫入許可權授與所有使用者。
如需一般 I/O 工作的清單,請參閱 一般 I/O 工作。
建構函式
DirectoryInfo(String) |
在指定的路徑上,初始化 DirectoryInfo 類別的新執行個體。 |
欄位
FullPath |
表示目錄或檔案的完整路徑。 (繼承來源 FileSystemInfo) |
OriginalPath |
使用者指定的相對或絕對的原始路徑。 (繼承來源 FileSystemInfo) |
屬性
Attributes |
取得或設定目前檔案或目錄的屬性。 (繼承來源 FileSystemInfo) |
CreationTime |
取得或設定目前檔案或目錄的建立時間。 (繼承來源 FileSystemInfo) |
CreationTimeUtc |
取得或設定目前檔案或目錄的建立時間,其格式為國際標準時間 (Coordinated Universal Time,UTC)。 (繼承來源 FileSystemInfo) |
Exists |
取得值,指出目錄是否存在。 |
Extension |
取得檔名的擴展名部分,包括前置點 |
FullName |
取得目錄的完整路徑。 |
FullName |
取得目錄或檔案的完整路徑。 (繼承來源 FileSystemInfo) |
LastAccessTime |
取得或設定上次存取目前檔案或目錄的時間。 (繼承來源 FileSystemInfo) |
LastAccessTimeUtc |
取得或設定目前檔案或目錄上次被存取的時間,其格式為 Coordinated Universal Time (UTC)。 (繼承來源 FileSystemInfo) |
LastWriteTime |
取得或設定上次寫入目前檔案或目錄的時間。 (繼承來源 FileSystemInfo) |
LastWriteTimeUtc |
取得或設定目前檔案或目錄上次被寫入的時間,其格式為 Coordinated Universal Time (UTC)。 (繼承來源 FileSystemInfo) |
LinkTarget |
取得位於 中 FullName之連結的目標路徑,如果這個實例不代表連結,則 |
Name |
取得這個 DirectoryInfo 執行個體的名稱。 |
Parent |
取得指定子目錄的父代 (Parent) 目錄。 |
Root |
取得目錄的根目錄部分。 |
UnixFileMode |
取得或設定目前檔案或目錄的 Unix 檔案模式。 (繼承來源 FileSystemInfo) |
方法
擴充方法
Create(DirectoryInfo, DirectorySecurity) |
建立新的目錄,確保其以指定的目錄安全性建立。 如果目錄已經存在,則不執行任何動作。 |
GetAccessControl(DirectoryInfo) |
傳回目錄的安全性資訊。 |
GetAccessControl(DirectoryInfo, AccessControlSections) |
傳回目錄的安全性資訊。 |
SetAccessControl(DirectoryInfo, DirectorySecurity) |
變更現有目錄的安全性屬性。 |