C# 目錄訊息

WBOY
發布: 2024-09-03 15:17:59
原創
352 人瀏覽過

C# Directoryinfo 允許我們處理目錄資料夾系統,DirectoryInfo 是System.IO 內部可用的類,或者只是命名空間System.IO 包含DirectoryInfo 類,DirectoryInfo 類別包含與FileInfo 類別幾乎相似的功能C#,唯一的區別是DirectoryInfo 只專注於目錄而不是檔案系統,當我們談論DirectoryInfo 類別時,我們總是談論實體目錄,在它的幫助下,我們獲得了可以建立的對象,刪除,我們還可以建立一些子目錄並執行更多操作。

文法

以下是實作 DirectoryInfo 類別的簡單語法。我們可以透過以下方式解釋以下語法。

  • 首先,我們定義了一個帶有 DirectoryInfo 類型變數的類別。
  • 我們正在使用新關鍵字來分配由 DirectoryInfo 建立的物件。
  • 我們可以看到這裡的語法,我們將物件建立的 dpath 傳遞給 DirectoryInfo 類別。
  • 這裡的 dpath 是任意路徑字串。
  • 最後,我們使用程式碼作為directory.create,它將建立目錄。
  • 請記住,我們也應該檢查目錄是否已存在。
//sourceDirectory: This is the string of the path or directory for which we want to perform certain operations.
DirectoryInfo directory = new DirectoryInfo(dPath);
directory.Create();
登入後複製

C# DirectoryInfo 類別的工作

我們可以透過以下方式解釋 DirectoryInfo 類別的工作原理。

  • 命名空間 Sytem.IO 包含 DirectoryInfo 類,因此如果我們想使用它,我們需要包含這個函式庫。
  • 最重要的是,透過使用可用的命令,我們可以建立和移動目錄。
  • 它有很多方法,這是 DirectoryInfo 的關鍵優勢,它允許我們執行建立和刪除。
  • 關於 DirectoryInfo 類別最重要的一點是我們不能繼承它,因為它是一個密封類別(我們可以在其文件中了解有關 C# 中密封類別的更多資訊)。
  • DirectoryInfo類別的繼承流程為Object ===> MarshalByRefObject ===>檔案系統資訊 ===> DirectoryInfo ,此依賴關係顯示它們如何從其父級繼承到 DirectoryInfo 類別。

C# DirectoryInfo 的建構子

建構函式中是DirectoryInfo類別的初始化方法。這裡我們需要傳遞路徑來初始化,路徑是我們要建立或移動的目錄的字串。

函數類型(私有/公有/受保護)DirectoryInfo(字串directoryPath);

屬性,

directoryPath:這是我們呼叫建構函式來為給定路徑建立物件以執行移動或建立操作的字串路徑。

C# DirectoryInfo 的方法

以下是下面提到的方法:

  • Create ( string ):如果我們想要建立一個新目錄,我們可以使用這個方法。在該方法中,我們傳遞一個字串,我們要為其建立目錄的字串路徑。
  • CreateSubdirectory:我們了解到我們可以藉助 create 方法建立目錄,現在如果我們想在另一個目錄(子目錄)中建立目錄怎麼辦?我們可以簡單地使用 CreateSubdirectory 方法。繞過此方法的字串路徑,我們也可以建立指定路徑的子目錄。
  • MoveTo:用於將目錄中的所有常數和實例移動到其他位置。
  • 刪除:它將刪除指定的目錄,繞過一個布林值,我們可以通知它的編譯器如果我們想刪除它的子目錄。
  • GetDirectories:要了解子目錄,我們可以使用此方法。在現實生活程式設計中,很多時候我們需要在刪除之前知道路徑名,因此它會非常有用,因為它提到了子目錄詳細資訊。
  • GetFiles: 如果我們想要從指定目錄取得文件,那麼我們可以使用 GetFile 方法。
  • GetType():了解實例的類型(目前)。
  • Refresh():要刷新物件狀態,我們可以使用 Refresh() 方法。
  • SetAccessControl:此方法主要用於安全原因,它將取得一個 DirectorySecurity 作為描述它的物件。
  • ToString():要取得使用者傳遞的原始路徑,我們可以使用方法 ToString()。

C# DirectoryInfo 的屬性

以下是下面提到的屬性

  • CreationTime: In case if we wanted to know the date and time of the directory creation then we can use the property CreationTime.
  • Exists: It returns the boolean value, which shows if the directory exists or not. In case if the directory is already there then it returns true ele it will return false.
  • FullName: If we wanted to get the full name of the file ( which means starting from root directory ).
  • Name: In this case, it used simply to get the name of the directory.
  • LastAccessTime: In case if we wanted to get the last date and time when the directory was modified then we can use this property.
  • LastWriteTime: If we wanted to get the last file changes and save the details of the changes.
  • Extension: It is used to get the string representing the extension part of the file.
  • Parent: In case if we wanted to get the parent directory name then we can use Parent. It will give us the parent directory name.

Example of C# DirectoryInfo

Below is a very simple example, here we are simply trying to create a directory, we are also checking if the directory already exists or not.

Please see the below example along with the screen of output.

Code:

using System.IO;
using System;
class createDirectory
{
static void Main()
{
string dPath = @"D:\directoryExample";
//Initialisation of the Object by passing the path
DirectoryInfo directory = new DirectoryInfo(dPath);
// Let us first check if the directory already exist or not
if (directory.Exists)
{
Console.WriteLine("The directory which you are trying to create is already there");
}
//If the directory which we are trying to create is not there
else
{
// below code will create the directory with name we have provided
directory.Create();
Console.WriteLine("Congratulation we have created directory");
}
Console.ReadLine();
}
}
登入後複製

Output:

C# 目錄訊息

Conclusion

From this tutorial, we learned about the DirectoryInfo in C# and we learned about the DirectoryInfo behaviors with a very important example. We learned about the constructors and methods of the DirectoryInfo. We understand the working of DirectoryInfo in C#.

以上是C# 目錄訊息的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!