Path.GetFullPath メソッド

定義

オーバーロード

GetFullPath(String)

指定したパス文字列の絶対パスを返します。

GetFullPath(String, String)

相対パスと完全修飾ベース パスから絶対パスを返します。

GetFullPath(String)

ソース:
Path.Unix.cs
ソース:
Path.Unix.cs
ソース:
Path.Unix.cs

指定したパス文字列の絶対パスを返します。

public:
 static System::String ^ GetFullPath(System::String ^ path);
public static string GetFullPath (string path);
static member GetFullPath : string -> string
Public Shared Function GetFullPath (path As String) As String

パラメーター

path
String

絶対パス情報を取得する対象のファイルまたはディレクトリ。

戻り値

"C:\MyFile.txt" など、path の完全修飾位置。

例外

path は長さ 0 の文字列、Windows システム上の空白のみを含む、または で GetInvalidPathChars()定義されている無効な文字の 1 つ以上を含みます。

または

システムが絶対パスを取得できませんでした。

呼び出し元に、必要なアクセス許可がありません。

pathnullです。

.NET Frameworkのみ: path ボリューム識別子の一部ではないコロン (":") が含まれています ("c:\"など)。

指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。

次の例では、Windows ベースの GetFullPath デスクトップ プラットフォームの メソッドを示します。

String^ fileName = "myfile.ext";
String^ path = "\\mydir\\";
String^ fullPath;
fullPath = Path::GetFullPath( path );
Console::WriteLine( "GetFullPath('{0}') returns '{1}'", path, fullPath );
fullPath = Path::GetFullPath( fileName );
Console::WriteLine( "GetFullPath('{0}') returns '{1}'", fileName, fullPath );

// Output is based on your current directory, except
// in the last case, where it is based on the root drive
// GetFullPath('mydir') returns 'C:\temp\Demo\mydir'
// GetFullPath('myfile.ext') returns 'C:\temp\Demo\myfile.ext'
// GetFullPath('\mydir') returns 'C:\mydir'
string fileName = "myfile.ext";
string path1 = @"mydir";
string path2 = @"\mydir";
string fullPath;

fullPath = Path.GetFullPath(path1);
Console.WriteLine("GetFullPath('{0}') returns '{1}'",
    path1, fullPath);

fullPath = Path.GetFullPath(fileName);
Console.WriteLine("GetFullPath('{0}') returns '{1}'",
    fileName, fullPath);

fullPath = Path.GetFullPath(path2);
Console.WriteLine("GetFullPath('{0}') returns '{1}'",
    path2, fullPath);

// Output is based on your current directory, except
// in the last case, where it is based on the root drive
// GetFullPath('mydir') returns 'C:\temp\Demo\mydir'
// GetFullPath('myfile.ext') returns 'C:\temp\Demo\myfile.ext'
// GetFullPath('\mydir') returns 'C:\mydir'
Dim fileName As string = "myfile.ext"
Dim path1 As string = "mydir"
Dim path2 As string = "\mydir"
Dim fullPath As string

fullPath = Path.GetFullPath(path1)
Console.WriteLine("GetFullPath('{0}') returns '{1}'", _
    path1, fullPath)

fullPath = Path.GetFullPath(fileName)
Console.WriteLine("GetFullPath('{0}') returns '{1}'", _
    fileName, fullPath)

fullPath = Path.GetFullPath(path2)
Console.WriteLine("GetFullPath('{0}') returns '{1}'", _
    path2, fullPath)

' Output is based on your current directory, except
' in the last case, where it is based on the root drive
' GetFullPath('mydir') returns 'C:\temp\Demo\mydir'
' GetFullPath('myfile.ext') returns 'C:\temp\Demo\myfile.ext'
' GetFullPath('\mydir') returns 'C:\mydir'

注釈

絶対パスには、システム上のファイルまたはディレクトリを検索するために必要なすべての情報が含まれます。

path 指定されたファイルまたはディレクトリが存在する必要はありません。 たとえば、c:\temp\newdir が現在のディレクトリである場合、test.txt などのファイル名で を呼び出すとGetFullPath 、c:\temp\newdir\test.txtが返されます。 ファイルが存在する必要はありません。

重要

が相対パスの場合 path 、このオーバーロードは、現在のドライブと現在のディレクトリに基づく完全修飾パスを返します。 現在のドライブと現在のディレクトリは、アプリケーションの実行時にいつでも変更できます。 その結果、このオーバーロードによって返されるパスを事前に決定することはできません。 決定論的パスを返すには、 オーバーロードを GetFullPath(String, String) 呼び出します。 また、 メソッドを IsPathFullyQualified 呼び出して、パスが完全修飾か相対かを判断し、 の呼び出し GetFullPath が必要かどうかを判断することもできます。

ただし、 が存在する場合 path 、呼び出し元には の pathパス情報を取得するためのアクセス許可が必要です。 クラスの Path ほとんどのメンバーとは異なり、このメソッドはファイル システムにアクセスすることに注意してください。

このメソッドは、現在のディレクトリと現在のボリューム情報を使用して を完全修飾 pathします。 でpathGetFullPathファイル名のみを指定すると、現在のディレクトリの完全修飾パスが返されます。

短いファイル名を渡すと、長いファイル名に展開されます。

パスに有効な文字が含まれていない場合は、1 つ以上の "." 文字の後に任意の数のスペースが続く場合を除き、無効です。その後、"." または ".." として解析されます。

.NET Core 1.1 以降のバージョンと .NET Framework 4.6.2 以降のバージョンでは、"\\?\C:\" などのデバイス名を含むパスもサポートされています。

Windows のファイル パス形式の詳細については、「Windows システムのファイル パス形式」を参照してください。 共通 I/O タスクの一覧は、 共通 I/O タスク を参照してください。

こちらもご覧ください

適用対象

GetFullPath(String, String)

ソース:
Path.Unix.cs
ソース:
Path.Unix.cs
ソース:
Path.Unix.cs

相対パスと完全修飾ベース パスから絶対パスを返します。

public:
 static System::String ^ GetFullPath(System::String ^ path, System::String ^ basePath);
public static string GetFullPath (string path, string basePath);
static member GetFullPath : string * string -> string
Public Shared Function GetFullPath (path As String, basePath As String) As String

パラメーター

path
String

basePath に連結する相対パス。

basePath
String

完全修飾パスの先頭。

戻り値

絶対パス。

例外

path または basePathnull です。

basePath が完全修飾パスではありません。

または

path または basePath が、GetInvalidPathChars() で定義されている無効な文字を 1 つ以上含んでいます。

次の例では、 basePathアプリケーションの現在のディレクトリを表す 変数 を定義します。 次に、 メソッドに GetFullPath 渡して、アプリケーションのデータ ディレクトリへの完全修飾パスを取得します。

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string basePath = Environment.CurrentDirectory;
        string relativePath = "./data/output.xml";
 
        // Unexpectedly change the current directory.
        Environment.CurrentDirectory = "C:/Users/Public/Documents/";
        
        string fullPath = Path.GetFullPath(relativePath, basePath);
        Console.WriteLine($"Current directory:\n   {Environment.CurrentDirectory}");
        Console.WriteLine($"Fully qualified path:\n   {fullPath}");
    }
}
// The example displays the following output:
//   Current directory:
//      C:\Users\Public\Documents
//   Fully qualified path:
//      C:\Utilities\data\output.xml
Imports System.IO

Module Program
    Public Sub Main()
        Dim basePath As String = Environment.CurrentDirectory
        Dim relativePath As String = "./data/output.xml"
 
        ' Unexpectedly change the current directory.
        Environment.CurrentDirectory = "C:/Users/Public/Documents/"
        
        Dim fullPath As String = Path.GetFullPath(relativePath, basePath)
        Console.WriteLine($"Current directory:\n   {Environment.CurrentDirectory}")
        Console.WriteLine($"Fully qualified path:\n   {fullPath}")
    End Sub
End Module
' The example displays the following output:
'   Current directory:
'      C:\Users\Public\Documents
'   Fully qualified path:
'      C:\Utilities\data\output.xml

注釈

が空のパスの場合 path 、 メソッドは を返します basePath。 が完全修飾パスの場合path、メソッドは メソッドにGetFullPath(String)pathされ、結果が返されます。

相対パスを使用する場合は、指定したボリュームとルート化されたディレクトリに基づいて決定論的パスを返すには、このメソッドを使用します。 現在のドライブ ディレクトリに基づくのではなく、 basePath 定義済みの を使用すると、現在のドライブとディレクトリの予期しない変更によって引き起こされる不要なファイル パスから保護されます。

適用対象