Path.GetFullPath メソッド

定義

オーバーロード

名前 説明
GetFullPath(String, String)

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

GetFullPath(String)

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

GetFullPath(String, String)

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

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が完全修飾パスの場合、メソッドはpathGetFullPath(String) メソッドに渡し、結果を返します。

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

適用対象

GetFullPath(String)

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

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 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が返されます。 ファイルが存在する必要はありません。

Important

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

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

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

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

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

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

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

こちらもご覧ください

適用対象