| 型 | フィールド | 内容 |
|---|---|---|
| Char | DirectorySeparatorChar | プラットフォーム固有の、ディレクトリ区切り文字
|
| Char | AltDirectorySeparatorChar | プラットフォーム固有の、ディレクトリ区切りの代替文字
|
| Char | VolumeSeparatorChar | プラットフォーム固有の、ボリューム区切り文字
|
Pathクラスのたいていのメソッドはファイルシステムを考慮せず、ファイルの実在も検証しません。
| メソッド | 取得できる情報 |
|---|---|
| GetFileName(String) | ファイル名 (拡張子あり) |
| GetFileNameWithoutExtension(String) | ファイル名 (拡張子なし) |
| GetPathRoot(String) | ルート ディレクトリ (root directory) |
| GetDirectoryName(String) | ディレクトリ名 |
| GetExtension(String) | 拡張子 |
| GetFullPath(String) | 絶対パス |
| @"dir\sample.txt" (相対パス) | @"c:\dir\sample.txt" | @"c:\dir\sample" | @"c:\dir\sample\" (末尾に'\') | |
|---|---|---|---|---|
| GetFileName() | "sample.txt" | "sample.txt" | "sample" | "" |
| GetFileNameWithoutExtension() | "sample" | "sample" | "sample" | "" |
| GetPathRoot() | "" | "c:\" | "c:\" | "c:\" |
| GetDirectoryName() | "dir" | "c:\dir" | "c:\dir" | "c:\dir\sample" |
| GetExtension() | ".txt" | ".txt" | "" | "" |
ディレクトリ名を"sample.txt"とすることも可能なため、その場合はこれらの結果は誤っていることになります。
パス文字列にディレクトリ名を指定した場合、末尾のディレクトリ名が削除されて返されます。
string path = @"c:\dir1\dir2\sample.txt"; string r1 = Path.GetDirectoryName(path); // "c:\dir1\dir2" string r2 = Path.GetDirectoryName(r1); // "c:\dir1"
短いファイル名 (short filename) を指定すると、長いファイル名 (long filename) で返されます。
string r1 = Path.GetDirectoryName(@"C:\Program Files\"); // "C:\Program Files" string r2 = Path.GetDirectoryName(@"C:\PROGRA~1\"); // "C:\Program Files"
指定のパス文字列の、絶対パスを得られます。
Path.GetFullPath("TEST.txt");
// "c:\Users\UserName\documents\visual studio 2015\Projects\test\test\bin\x86\Debug\TEST.txt"
これの基準となる現在の作業ディレクトリは、Directory.GetCurrentDirectory()で得られます。
ディレクトリ名だけを取得するメソッドは提供されていないため、独自の実装が必要です。c# - Getting the folder name from a full filename path - Stack Overflow
string r1 = Path.GetFileName(Path.GetDirectoryName(path)); // 方法1 string r2 = new DirectoryInfo(path).Name; // 方法2 string r3 = new DirectoryInfo(Path.GetDirectoryName(path)).Name; // 方法3 // 方法4 int last1 = path.LastIndexOfAny(new[] { '\\', '/' }); int last2 = path.LastIndexOfAny(new[] { '\\', '/' }, last1 - 1); string r4 = path.Substring(last2 + 1, last1 - last2 - 1);
| 方法 | @"c:\dir\sample.txt" | @"c:\dir\" | @"c:\dir" | @"c:\dir.\sample.txt" | @"c:\\dir\\" | @"c:/dir/ |
|---|---|---|---|---|---|---|
| 1 | "dir" | "dir" | "" | "dir." | "dir" | "dir" |
| 2 | "sample.txt" | "dir" | "dir" | "sample.txt" | "dir" | "dir" |
| 3 | "dir" | "dir" | "c:\" | "dir" | "dir" | "dir" |
| 4 | "dir" | "dir" | "c:" | "dir." | "" | "dir" |
下表のメソッドで無効な文字を取得できますが、それらはファイルシステムに依存するため、正確性は保証されません。Remarks - Path.GetInvalidFileNameChars Method (System.IO) | Microsoft Learn
| メソッド | 機能 |
|---|---|
| GetInvalidFileNameChars() | ファイル名に使用できない文字 (禁止文字) を含む配列を取得できる |
| GetInvalidPathChars() | パス名に使用できない文字を含む配列を取得できる |
public static string GetTempPath()Path.GetTempPath メソッド (System.IO) | MSDN
現在のユーザーの一時フォルダのパスを取得できます。どのパスを用いるかは、以下の順で決定されます。
public static string GetTempFileName()Path.GetTempFileName メソッド (System.IO) | MSDN
public static string GetRandomFileName()Path.GetRandomFileName メソッド (System.IO) | MSDN
kpvfojym.mqyやsdovvkjv.nfqといった名前文字列をパスとして結合できます。
public static string Combine (string path1, string path2);Combine(String, String) - Path.Combine Method (System.IO) | Microsoft Learn
string path1 = Path.Combine("c:\\dir", "sample.txt"); // "c:\\dir\\sample.txt"
string path2 = Path.Combine("c:\\dir\\", "sample.txt"); // "c:\\dir\\sample.txt"
現在の作業ディレクトリと結合して絶対パスとしたいならば、GetFullPath()でも可能です。
先頭のパスは絶対パスであり、それ以降は相対パスであることが想定されています。Remarks - Path.Combine Method (System.IO) | Microsoft Learn
string path3 = Path.Combine("dir", "sample.txt"); // "dir\\sample.txt"
string path4 = Path.Combine("dir", "c:\\dir\\sample.txt"); // "c:\\dir\\sample.txt"
string path5 = Path.Combine("dir", ""); // "dir"
string path6 = Path.Combine("dir\\", ""); // "dir\\"
string path7 = Path.Combine("dir", "\\"); // "\\"
文字列はパス区切りで結合されるため、拡張子を渡すとそれもそのように結合されます。
string path8 = Path.Combine("sample", ".txt"); // "sample\\.txt"
内部では文字列にパスとして無効な文字が含まれていないか確認され、最初の引数の末尾に区切り文字 (DirectorySeparatorChar、AltDirectorySeparatorCharまたはVolumeSeparatorChar) が含まれていないならば、ディレクトリ区切り文字が追加されて文字列が結合されます。Combine - path.cs
パス文字列としての検証が不要ならばこのメソッドを用いる必要はなく、文字列として連結します。
文字列が、ドライブまたはUNCに固定されたパスかどうか判定できます。 Remarks - Path.IsPathRooted Method (System.IO) | Microsoft Learn c# - Check if full path given - Stack Overflow
bool r1 = Path.IsPathRooted(@"C:\dir"); // true bool r2 = Path.IsPathRooted(@"\\ComputerName\dir"); // true (UNC表記) bool r3 = Path.IsPathRooted(@"\dir"); // true (ルートからの相対パス) bool r4 = Path.IsPathRooted(@"dir"); // false (作業ディレクトリからの相対パス)