[Tips][フォルダー操作] 指定したパスの親フォルダーを取得する

スポンサーリンク

指定したパスの親フォルダーを取得するにはDirectoryクラスGetParentメソッドを使用します。

GetParentメソッドの戻り値はDirectoryInfo型となっており、返された値のNameプロパティに親フォルダー名が格納されています。

下記は”C:\Work\Child\Test.txt”の親フォルダー(Child)を取得する例です。

VBの例

Dim strPath As String = "C:Work\Child\Test.txt"
Dim strParentDir As IO.DirectoryInfo

'C:\Work\Child\Test.txt の親フォルダーを取得
strParentDir = IO.Directory.GetParent(strPath)

MessageBox.Show(strParentDir.Name)

C#の例

string strPath = @"C:Work\Child\Test.txt";
System.IO.DirectoryInfo strParentDir;

// C:\Work\Child\Test.txt の親フォルダーを取得
strParentDir = System.IO.Directory.GetParent(strPath);

MessageBox.Show(strParentDir.Name);
Please follow and like us:

コメント

タイトルとURLをコピーしました