Path クラス
アセンブリ: mscorlib (mscorlib.dll 内)
構文
解説
.NET Framework では、"\\.\PHYSICALDRIVE0" など、デバイス名を指定したパスを使用して物理ディスクに直接アクセスすることはできません。
パスとは、ファイルまたはディレクトリの位置を示す文字列です。パスは、必ずしもディスク上の位置をポイントせず、たとえば、メモリやデバイスの位置を示す場合もあります。パスの正しい書式は、現在のプラットフォームで決定されます。たとえば、システムによっては、パスをドライブ名やボリューム名で始める場合がありますが、この要素は他のシステムには存在しません。また、システムによっては、パスに、ファイルに格納された情報のタイプを示す拡張子を含めることができます。ファイル名の拡張子の書式は、プラットフォームに依存します。たとえば、拡張子を 3 文字に制限しているシステムもありますが、制限しないシステムもあります。現在のプラットフォームは、パスの要素を区切る文字セット、およびパスの指定時に使用できない文字セットも決定します。これらの違いがあるため、Path クラスのいくつかのメンバの厳密な動作と Path クラスのフィールドは、プラットフォームに依存します。
パスには、絶対位置情報または相対位置情報を含めることができます。絶対パスは、完全な位置を指定します。つまり、ファイルまたはディレクトリは、現在の位置に関係なく一意に識別できます。相対パスは、部分的な位置を指定します。つまり、現在の位置は、相対パスで指定したファイルを検索するときの開始点として使用されます。現在のディレクトリを確認するには、Directory.GetCurrentDirectory を呼び出します。
Path クラスのほとんどのメンバは、ファイル システムと対話せず、パス文字列で指定したファイルの有無を検査しません。ChangeExtension などのパス文字列を修正する Path クラス メンバは、ファイル システムのファイル名には影響を与えません。ただし、Path メンバは、指定したパス文字列の内容を検証し、文字列に、InvalidPathChars で定義されるパス文字列で無効な文字が含まれている場合は、ArgumentException をスローします。たとえば、Windows ベースのデスクトップ プラットフォームでは、引用符 (")、不等号 (より小) (<)、不等号 (より大) (>)、パイプ (|)、バックスペース (\b)、null (\0)、Unicode 文字の 16 ~ 18 および 20 ~ 25 が無効なパス文字です。
Path クラスのメンバを使用すると、ファイル名の拡張子がパスの一部かどうかの判断や、2 つの文字列を 1 つのパス名に結合するなどの一般的な操作をすばやく、簡単に実行できます。
Path クラスのすべてのメンバは静的であるため、パスのインスタンスを持たなくても呼び出すことができます。
メモ |
---|
入力文字列としてパスを受け入れるメンバでは、そのパスが正しい書式である必要があります。それ以外の場合は、例外が発生します。たとえば、パスが絶対パスであっても空白で始まっている場合、そのパスはクラスのメソッドではトリムされません。このため、パスが正しい書式にならず、例外が発生します。同様に、1 つのパスまたは複数のパスの組み合わせを絶対パスとして 2 度指定することはできません。たとえば、"c:\temp c:\windows" でも、ほとんどの場合において例外が発生します。パス文字列を受け入れるメソッドを使用するときは、パスが適切な書式であることを確認します。 |
パスを受け入れるメンバでは、ファイルまたはディレクトリを参照するパスを指定できます。指定するパスは、相対パス、またはサーバーおよび共有名を示す UNC (Universal Naming Convention) パスにすることができます。たとえば、次に示すパスはすべて有効なパスです。
-
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"。
これらの操作はすべて文字列で実行されるため、実行結果がすべてのシナリオで有効かどうかを確認できません。たとえば、GetExtension メソッドは、それに渡す文字列を解析し、その文字列から拡張子を返します。ただし、その拡張子を持つファイルがディスク上に存在するとは限りません。
このクラスの使用例については、以下の「使用例」を参照してください。その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。
GetExtension | |
GetFullPath | |
GetFileName | |
GetFileNameWithoutExtension | |
GetDirectoryName | |
ChangeExtension | |
Imports System Imports System.IO Public Class Test Public Shared Sub Main() Dim path1 As String = "c:\temp\MyTest.txt" Dim path2 As String = "c:\temp\MyTest" Dim path3 As String = "temp" If Path.HasExtension(path1) Then Console.WriteLine("{0} has an extension.", path1) End If If Path.HasExtension(path2) = False Then Console.WriteLine("{0} has no extension.", path2) End If If Path.IsPathRooted(path3) = False Then Console.WriteLine("The string {0} contains no root information.", path3) End If Console.WriteLine("The full path of {0} is {1}.", path3, Path.GetFullPath(path3)) Console.WriteLine("{0} is the location for temporary files.", Path.GetTempPath()) Console.WriteLine("{0} is a file available for use.", Path.GetTempFileName()) ' This code produces output similar to the following: ' c:\temp\MyTest.txt has an extension. ' c:\temp\MyTest has no extension. ' The string temp contains no root information. ' The full path of temp is D:\Documents and Settings\cliffc\My Documents\Visual Studio 2005\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\temp. ' D:\Documents and Settings\cliffc\Local Settings\Temp\8\ is the location for temporary files. ' D:\Documents and Settings\cliffc\Local Settings\Temp\8\tmp3D.tmp is a file available for use. End Sub End Class
using System; using System.IO; class Test { public static void Main() { string path1 = @"c:\temp\MyTest.txt"; string path2 = @"c:\temp\MyTest"; string path3 = @"temp"; if (Path.HasExtension(path1)) { Console.WriteLine("{0} has an extension.", path1); } if (!Path.HasExtension(path2)) { Console.WriteLine("{0} has no extension.", path2); } if (!Path.IsPathRooted(path3)) { Console.WriteLine("The string {0} contains no root information.", path3); } Console.WriteLine("The full path of {0} is {1}.", path3, Path.GetFullPath(path3)); Console.WriteLine("{0} is the location for temporary files.", Path.GetTempPath()); Console.WriteLine("{0} is a file available for use.", Path.GetTempFileName()); /* This code produces output similar to the following: * c:\temp\MyTest.txt has an extension. * c:\temp\MyTest has no extension. * The string temp contains no root information. * The full path of temp is D:\Documents and Settings\cliffc\My Documents\Visual Studio 2005\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\temp. * D:\Documents and Settings\cliffc\Local Settings\Temp\8\ is the location for temporary files. * D:\Documents and Settings\cliffc\Local Settings\Temp\8\tmp3D.tmp is a file available for use. */ } }
using namespace System; using namespace System::IO; int main() { String^ path1 = "c:\\temp\\MyTest.txt"; String^ path2 = "c:\\temp\\MyTest"; String^ path3 = "temp"; if ( Path::HasExtension( path1 ) ) { Console::WriteLine( "{0} has an extension.", path1 ); } if ( !Path::HasExtension( path2 ) ) { Console::WriteLine( "{0} has no extension.", path2 ); } if ( !Path::IsPathRooted( path3 ) ) { Console::WriteLine( "The string {0} contains no root information.", path3 ); } Console::WriteLine( "The full path of {0} is {1}.", path3, Path::GetFullPath( path3 ) ); Console::WriteLine( "{0} is the location for temporary files.", Path::GetTempPath() ); Console::WriteLine( "{0} is a file available for use.", Path::GetTempFileName() ); Console::WriteLine( "\r\nThe set of invalid characters in a path is:" ); Console::WriteLine( "(Note that the wildcard characters '*' and '?' are not invalid.):" ); Collections::IEnumerator^ myEnum = Path::InvalidPathChars->GetEnumerator(); while ( myEnum->MoveNext() ) { Char c = *safe_cast<Char^>(myEnum->Current); Console::WriteLine( c ); } }
import System.*; import System.IO.*; class Test { public static void main(String[] args) { String path1 = "c:\\temp\\MyTest.txt"; String path2 = "c:\\temp\\MyTest"; String path3 = "temp"; if (Path.HasExtension(path1)) { Console.WriteLine("{0} has an extension.", path1); } if (!(Path.HasExtension(path2))) { Console.WriteLine("{0} has no extension.", path2); } if (!(Path.IsPathRooted(path3))) { Console.WriteLine("The string {0} contains no root information.", path3); } Console.WriteLine("The full path of {0} is {1}.", path3, Path.GetFullPath(path3)); Console.WriteLine("{0} is the location for temporary files.", Path.GetTempPath()); Console.WriteLine("{0} is a file available for use.", Path.GetTempFileName()); Console.WriteLine("\r\nThe set of invalid characters in a path is:"); Console.WriteLine("(Note that the wildcard characters '*' and '?' " + "are not invalid.):"); char c = ' '; for (int iCtr = 0; iCtr < Path.InvalidPathChars.get_Length(); iCtr++) { c = Path.InvalidPathChars[iCtr]; Console.WriteLine(c); } } //main } //Test
System.IO.Path
プラットフォーム
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Path フィールド
Path メソッド
パブリック メソッド
Path メンバ
ファイルまたはディレクトリのパス情報を格納する String インスタンスで操作を実行します。これらの操作は、プラットフォーム間で実行されます。
パブリック フィールド
パブリック メソッド
Weblioに収録されているすべての辞書からPATHを検索する場合は、下記のリンクをクリックしてください。
全ての辞書からPATHを検索
- PATHのページへのリンク