ルートディレクトリを取得する
スポンサーリンク
ファイルパスから、ルートディレクトリ (ドライブ レター) を取得するには、System.IO.Path クラスの GetPathRoot メソッドを使用します。
サンプルコード
以下にサンプルコードを示します。
C# 全般
// ルートディレクトリ名を取得する
string stRootName = System.IO.Path.GetPathRoot(@"C:\Hoge\Foo.txt");
// ルートディレクトリ名を表示する
MessageBox.Show(stRootName);
DirectoryInfo からならば、ルートディレクトリの DirectoryInfo が取得できます。
C# 全般
// DirectoryInfo の新しいインスタンスを生成する
System.IO.DirectoryInfo hDirInfo = new System.IO.DirectoryInfo(@"C:\Hoge\Bar\");
// DirectoryInfo からルートディレクトリの DirectoryInfo を取得する
System.IO.DirectoryInfo hRootInfo = hDirInfo.Root;
// ルートディレクトリ名を表示する
MessageBox.Show(hRootInfo.FullName);
関連するリファレンス
準備中です。
