Path クラスとは? わかりやすく解説

Path クラス

ファイルまたはディレクトリパス情報格納する String インスタンス操作実行します。これらの操作は、プラットフォーム間で実行されます。

名前空間: System.IO
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<ComVisibleAttribute(True)> _
Public NotInheritable Class
 Path
[ComVisibleAttribute(true)] 
public static class Path
[ComVisibleAttribute(true)] 
public ref class Path abstract sealed
/** @attribute ComVisibleAttribute(true) */ 
public final class Path
ComVisibleAttribute(true) 
public final class Path
解説解説

.NET Framework では、"\\.\PHYSICALDRIVE0" など、デバイス名を指定したパス使用して物理ディスク直接アクセスすることはできません。

パスとは、ファイルまたはディレクトリ位置を示す文字列です。パスは、必ずしもディスク上の位置ポイントせず、たとえば、メモリデバイス位置を示す場合ありますパス正し書式は、現在のプラットフォーム決定されます。たとえば、システムによっては、パスドライブ名ボリューム名で始め場合ありますが、この要素他のシステムには存在しません。また、システムによっては、パスに、ファイル格納され情報タイプを示す拡張子含めることができますファイル名拡張子書式は、プラットフォーム依存します。たとえば、拡張子を 3 文字制限しているシステムありますが、制限しないシステムあります現在のプラットフォームは、パス要素区切る文字セット、およびパス指定時に使用できない文字セット決定します。これらの違いがあるため、Path クラスいくつかのメンバ厳密な動作Path クラスフィールドは、プラットフォーム依存します

パスには、絶対位置情報または相対位置情報含めることができます絶対パスは、完全な位置指定します。つまり、ファイルまたはディレクトリは、現在の位置に関係なく一意識別できます相対パスは、部分的な位置指定します。つまり、現在の位置は、相対パス指定したファイル検索するときの開始点として使用されます。現在のディレクトリ確認するには、Directory.GetCurrentDirectory を呼び出します。

Path クラスのほとんどのメンバは、ファイル システム対話せず、パス文字列指定したファイル有無検査しません。ChangeExtension などのパス文字列修正する Path クラス メンバは、ファイル システムファイル名には影響与えません。ただし、Path メンバは、指定したパス文字列内容検証し文字列に、InvalidPathChars で定義されるパス文字列無効な文字含まれている場合は、ArgumentException をスローます。たとえば、Windows ベースデスクトップ プラットフォームでは、引用符 (")、不等号 (より小) (<)、不等号 (より大) (>)、パイプ (|)、バックスペース (\b)、null (\0)、Unicode 文字1618 および 2025無効なパス文字です。

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 タスクの例を次の表に示します

使用例使用例

Path クラス主要なメンバコード例次に示します

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.Object
  System.IO.Path
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Path クラス」の関連用語

Path クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Path クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS