Path.Join メソッド
定義
オーバーロード
Join(String, String, String, String) |
4 つのパスを単一のパスに連結します。Concatenates four paths into a single path. |
Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>) |
4 つのパス コンポーネントを単一のパスに連結します。Concatenates four path components into a single path. |
Join(String, String, String) |
3 つのパスを単一のパスに連結します。Concatenates three paths into a single path. |
Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>) |
2 つのパス コンポーネントを単一のパスに連結します。Concatenates two path components into a single path. |
Join(String, String) |
2 つのパスを単一のパスに連結します。Concatenates two paths into a single path. |
Join(String[]) |
パスの配列を連結して 1 つのパスにします。Concatenates an array of paths into a single path. |
Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>) |
3 つのパス コンポーネントを単一のパスに連結します。Concatenates three path components into a single path. |
Join(String, String, String, String)
4 つのパスを単一のパスに連結します。Concatenates four paths into a single path.
public:
static System::String ^ Join(System::String ^ path1, System::String ^ path2, System::String ^ path3, System::String ^ path4);
public static string? Join (string path1, string path2, string path3, string path4);
static member Join : string * string * string * string -> string
Public Shared Function Join (path1 As String, path2 As String, path3 As String, path4 As String) As String
パラメーター
- path1
- String
結合する 1 番目のパス。The first path to join.
- path2
- String
結合する 2 番目のパス。The second path to join.
- path3
- String
結合する 3 番目のパス。The third path to join.
- path4
- String
結合する 4 番目のパス。The fourth path to join.
戻り値
連結されたパス。The concatenated path.
注釈
このメソッドは、、、およびを連結するだけで、 path
path2
path3
path4
パスコンポーネントがまだ存在しない場合は、そのディレクトリの区切り文字を追加します。This method simply concatenates path
, path2
, path3
and path4
and adds a directory separator character between any of the path components if one is not already present. 、、またはのいずれかの引数の長さ path1
path2
が0の場合 path3
path4
、メソッドは残りの引数を連結します。If the length of any of path1
, path2
, path3
or path4
argument is zero, the method concatenates the remaining arguments. 結果として連結された文字列の長さがゼロの場合、メソッドはを返し String.Empty ます。If the length of the resulting concatenated string is zero, the method returns String.Empty.
path1
また path2
はまたはが、 path3
ターゲットプラットフォームに適していないパス区切り文字で終了した場合、 Join
メソッドは元のパス区切り文字を保持し、サポートされているものを追加します。If path1
or path2
or path3
ends in a path separator character that is not appropriate for the target platform, the Join
method preserves the original path separator character and appends the supported one. この問題 \ は、Unix ベースのシステムでパスの区切りとして認識されない Windows の円記号 ("") 文字を使用するハードコードされたパスで発生します。This issue arises in hard-coded paths that use the Windows backslash ("\") character, which is not recognized as a path separator on Unix-based systems. この問題を回避するには、次の操作を行います。To work around this issue, you can:
Path.DirectorySeparatorCharディレクトリの区切り文字をハードコーディングするのではなく、プロパティの値を取得します。Retrieve the value of the Path.DirectorySeparatorChar property rather than hard-coding a directory separator character.
ディレクトリの区切り記号としてスラッシュ ("/") を使用します。Use a forward slash ("/") as the directory separator character. この文字は、 Path.DirectorySeparatorChar Unix ベースのシステムのプロパティと Windows システムのプロパティによって返され Path.AltDirectorySeparatorChar ます。This character is returned by the Path.DirectorySeparatorChar property on Unix-based systems and by the Path.AltDirectorySeparatorChar property on Windows systems.
メソッドとは異なり Combine 、 Join メソッドは返されたパスのルートを試行しません。Unlike the Combine method, the Join method does not attempt to root the returned path. (またはまたはが絶対パスの場合、メソッドは、 path2
path2
メソッドが path3
Join
行ったように、前のパスを破棄しません Combine 。(That is, if path2
or path2
or path3
is an absolute path, the Join
method does not discard the previous paths as the Combine method does.
Join
検索ワイルドカード文字にはこれらの文字を使用できるため、ディレクトリ名とファイル名に無効な文字が含まれることはありません。Not all invalid characters for directory and file names are interpreted as unacceptable by the Join
method, because you can use these characters for search wildcard characters. たとえば、 Path.Join("c:\\", "temp", "*.txt")
ファイルの作成時には無効になる場合がありますが、検索文字列として有効です。For example, while Path.Join("c:\\", "temp", "*.txt")
might be invalid when creating a file, it is valid as a search string. その Join
ため、メソッドは正常に解釈します。The Join
method therefore successfully interprets it.
適用対象
Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)
4 つのパス コンポーネントを単一のパスに連結します。Concatenates four path components into a single path.
public:
static System::String ^ Join(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2, ReadOnlySpan<char> path3, ReadOnlySpan<char> path4);
public static string? Join (ReadOnlySpan<char> path1, ReadOnlySpan<char> path2, ReadOnlySpan<char> path3, ReadOnlySpan<char> path4);
static member Join : ReadOnlySpan<char> * ReadOnlySpan<char> * ReadOnlySpan<char> * ReadOnlySpan<char> -> string
Public Shared Function Join (path1 As ReadOnlySpan(Of Char), path2 As ReadOnlySpan(Of Char), path3 As ReadOnlySpan(Of Char), path4 As ReadOnlySpan(Of Char)) As String
パラメーター
- path1
- ReadOnlySpan<Char>
結合する最初のパスを含む文字範囲。A character span that contains the first path to join.
- path2
- ReadOnlySpan<Char>
結合する 2 番目のパスを含む文字範囲。A character span that contains the second path to join.
- path3
- ReadOnlySpan<Char>
結合する 3 番目のパスを含む文字範囲。A character span that contains the third path to join.
- path4
- ReadOnlySpan<Char>
結合する 4 番目のパスを含む文字範囲。A character span that contains the fourth path to join.
戻り値
連結されたパス。The concatenated path.
注釈
このメソッドは、、、およびを連結するだけで、 path
path2
path3
path4
パスコンポーネントがまだ存在しない場合は、そのディレクトリの区切り文字を追加します。This method simply concatenates path
, path2
, path3
and path4
and adds a directory separator character between any of the path components if one is not already present. Length、、またはのいずれかの引数のが0の場合 path1
path2
path3
path4
、メソッドは残りの引数を連結します。If the Length of any of path1
, path2
, path3
or path4
argument is zero, the method concatenates the remaining arguments. ReadOnlySpan<T>.Lengthすべてのコンポーネントのがゼロの場合、メソッドはを返し String.Empty ます。If the ReadOnlySpan<T>.Length of all components is zero, the method returns String.Empty.
path1
また path2
はまたはが、 path3
ターゲットプラットフォームに適していないパス区切り文字で終了した場合、 Join
メソッドは元のパス区切り文字を保持し、サポートされているものを追加します。If path1
or path2
or path3
ends in a path separator character that is not appropriate for the target platform, the Join
method preserves the original path separator character and appends the supported one. この問題 \ は、Unix ベースのシステムでパスの区切りとして認識されない Windows の円記号 ("") 文字を使用するハードコードされたパスで発生します。This issue arises in hard-coded paths that use the Windows backslash ("\") character, which is not recognized as a path separator on Unix-based systems. この問題を回避するには、次の操作を行います。To work around this issue, you can:
Path.DirectorySeparatorCharディレクトリの区切り文字をハードコーディングするのではなく、プロパティの値を取得します。Retrieve the value of the Path.DirectorySeparatorChar property rather than hard-coding a directory separator character.
ディレクトリの区切り記号としてスラッシュ ("/") を使用します。Use a forward slash ("/") as the directory separator character. この文字は、 Path.DirectorySeparatorChar Unix ベースのシステムのプロパティと Windows システムのプロパティによって返され Path.AltDirectorySeparatorChar ます。This character is returned by the Path.DirectorySeparatorChar property on Unix-based systems and by the Path.AltDirectorySeparatorChar property on Windows systems.
メソッドとは異なり Combine 、 Join メソッドは返されたパスのルートを試行しません。Unlike the Combine method, the Join method does not attempt to root the returned path. (またはまたはが絶対パスの場合、メソッドは、 path2
path2
メソッドが path3
Join
行ったように、前のパスを破棄しません Combine 。(That is, if path2
or path2
or path3
is an absolute path, the Join
method does not discard the previous paths as the Combine method does.
Join
検索ワイルドカード文字にはこれらの文字を使用できるため、ディレクトリ名とファイル名に無効な文字が含まれることはありません。Not all invalid characters for directory and file names are interpreted as unacceptable by the Join
method, because you can use these characters for search wildcard characters. たとえば、 Path.Join("c:\\", "temp", "*.txt")
ファイルの作成時には無効になる場合がありますが、検索文字列として有効です。For example, while Path.Join("c:\\", "temp", "*.txt")
might be invalid when creating a file, it is valid as a search string. その Join
ため、メソッドは正常に解釈します。The Join
method therefore successfully interprets it.
適用対象
Join(String, String, String)
3 つのパスを単一のパスに連結します。Concatenates three paths into a single path.
public:
static System::String ^ Join(System::String ^ path1, System::String ^ path2, System::String ^ path3);
public static string? Join (string path1, string path2, string path3);
static member Join : string * string * string -> string
Public Shared Function Join (path1 As String, path2 As String, path3 As String) As String
パラメーター
- path1
- String
結合する 1 番目のパス。The first path to join.
- path2
- String
結合する 2 番目のパス。The second path to join.
- path3
- String
結合する 3 番目のパス。The third path to join.
戻り値
連結されたパス。The concatenated path.
注釈
このメソッドは、、、およびを連結するだけで、 path
path2
path3
パスコンポーネントがまだ存在しない場合は、そのディレクトリの区切り文字を追加します。This method simply concatenates path
, path2
, and path3
and adds a directory separator character between any of the path components if one is not already present. 、、またはのいずれかの引数の長さ path1
path2
が0の場合 path3
、メソッドは残りの引数を連結します。If the length of any of path1
, path2
or path3
argument is zero, the method concatenates the remaining arguments. 結果として連結された文字列の長さがゼロの場合、メソッドはを返し String.Empty ます。If the length of the resulting concatenated string is zero, the method returns String.Empty.
path1
またはが path2
、ターゲットプラットフォームに適していないパスの区切り文字で終わった場合、 Join
メソッドは元のパス区切り文字を保持し、サポートされているものを追加します。If path1
or path2
ends in a path separator character that is not appropriate for the target platform, the Join
method preserves the original path separator character and appends the supported one. この問題 \ は、Unix ベースのシステムでパスの区切りとして認識されない Windows の円記号 ("") 文字を使用するハードコードされたパスで発生します。This issue arises in hard-coded paths that use the Windows backslash ("\") character, which is not recognized as a path separator on Unix-based systems. この問題を回避するには、次の操作を行います。To work around this issue, you can:
Path.DirectorySeparatorCharディレクトリの区切り文字をハードコーディングするのではなく、プロパティの値を取得します。Retrieve the value of the Path.DirectorySeparatorChar property rather than hard-coding a directory separator character.
ディレクトリの区切り記号としてスラッシュ ("/") を使用します。Use a forward slash ("/") as the directory separator character. この文字は、 Path.DirectorySeparatorChar Unix ベースのシステムのプロパティと Windows システムのプロパティによって返され Path.AltDirectorySeparatorChar ます。This character is returned by the Path.DirectorySeparatorChar property on Unix-based systems and by the Path.AltDirectorySeparatorChar property on Windows systems.
メソッドとは異なり Combine 、 Join メソッドは返されたパスのルートを試行しません。Unlike the Combine method, the Join method does not attempt to root the returned path. (またはが絶対パスの場合、メソッドは、 path2
path2
メソッドが Join
行ったように、前のパスを破棄しません Combine 。(That is, if path2
or path2
is an absolute path, the Join
method does not discard the previous paths as the Combine method does.
Join
検索ワイルドカード文字にはこれらの文字を使用できるため、ディレクトリ名とファイル名に無効な文字が含まれることはありません。Not all invalid characters for directory and file names are interpreted as unacceptable by the Join
method, because you can use these characters for search wildcard characters. たとえば、 Path.Join("c:\\", "temp", "*.txt")
ファイルの作成時には無効になる場合がありますが、検索文字列として有効です。For example, while Path.Join("c:\\", "temp", "*.txt")
might be invalid when creating a file, it is valid as a search string. その Join
ため、メソッドは正常に解釈します。The Join
method therefore successfully interprets it.
適用対象
Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>)
2 つのパス コンポーネントを単一のパスに連結します。Concatenates two path components into a single path.
public:
static System::String ^ Join(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2);
public static string? Join (ReadOnlySpan<char> path1, ReadOnlySpan<char> path2);
public static string Join (ReadOnlySpan<char> path1, ReadOnlySpan<char> path2);
static member Join : ReadOnlySpan<char> * ReadOnlySpan<char> -> string
Public Shared Function Join (path1 As ReadOnlySpan(Of Char), path2 As ReadOnlySpan(Of Char)) As String
パラメーター
- path1
- ReadOnlySpan<Char>
結合する最初のパスを含む文字範囲。A character span that contains the first path to join.
- path2
- ReadOnlySpan<Char>
結合する 2 番目のパスを含む文字範囲。A character span that contains the second path to join.
戻り値
結合されたパス。The combined paths.
例
次の例は、メソッドとメソッドによって返されるパスの違いを示してい Path.Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>) Path.Combine(String, String) ます。The following example illustrates the difference in the paths returned by the Path.Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>) and Path.Combine(String, String) methods. 最初の文字列がドライブおよびルートディレクトリを含む完全修飾パスで、2番目の文字列が最初のパスからの相対パスである場合、2つのメソッドは同じ結果を生成します。When the first string is an fully qualified path that includes a drive and root directory and the second is a relative path from the first path, the two methods produce identical results. 2回目と3回目のメソッドの呼び出しでは、 ShowPathInformation
2 つのメソッドによって返される文字列は分岐します。In the second and third calls to the ShowPathInformation
method, the strings returned by the two methods diverge. 2番目のメソッド呼び出しでは、最初の文字列引数はドライブ、2番目の文字列はルート化されたディレクトリです。In the second method call, the first string argument is a drive, while the second is a rooted directory. メソッドは、 Join
2 つの文字列を連結し、重複するパス区切り記号を保持します。The Join
method concatenates the two strings and preserves duplicate path separators. メソッドは、 Combine
ドライブを破棄し、現在のドライブ上のルート化されたディレクトリを返します。The Combine
method abandons the drive and returns a rooted directory on the current drive. アプリケーションの現在のドライブが C:\ の場合また、この文字列は、ディレクトリ内のファイルにアクセスするために使用され、d: ではなく C: にアクセスします。If the application's current drive is C:\ and the string is used to access a file or files in the directory, it would access C: instead of D:. 最後に、の3番目の呼び出しの引数は両方ともルート化されているため ShowPathInformation
、 Join
メソッドは単にそれらを追加して無意味ファイルパスを作成 Combine
します。メソッドは、最初の文字列を破棄し、2番目の文字列を返します。Finally, because both arguments in the third call to ShowPathInformation
are rooted, the Join
method simply appends them to create a nonsensical file path, while the Combine
method discards the first string and returns the second. ファイルアクセスにこの文字列を使用すると、アプリケーションが機密ファイルに意図せずアクセスする可能性があります。Using this string for file access could give the application unintended access to sensitive files.
using System;
using System.IO;
class Program
{
static void Main()
{
var path1 = "C:/Program Files/";
var path2 = "Utilities/SystemUtilities";
ShowPathInformation(path1, path2);
path1 = "C:/";
path2 = "/Program Files";
ShowPathInformation(path1, path2);
path1 = "C:/Users/Public/Documents/";
path2 = "C:/Users/User1/Documents/Financial/";
ShowPathInformation(path1, path2);
}
private static void ShowPathInformation(string path1, string path2)
{
var result = Path.Join(path1.AsSpan(), path2.AsSpan());
Console.WriteLine($"Concatenating '{path1}' and '{path2}'");
Console.WriteLine($" Path.Join: '{result}'");
Console.WriteLine($" Path.Combine: '{Path.Combine(path1, path2)}'");
}
}
// The example displays the following output if run on a Windows system:
// Concatenating 'C:/Program Files/' and 'Utilities/SystemUtilities'
// Path.Join: 'C:/Program Files/Utilities/SystemUtilities'
// Path.Combine: 'C:/Program Files/Utilities/SystemUtilities'
//
// Concatenating 'C:/' and '/Program Files'
// Path.Join: 'C://Program Files'
// Path.Combine: '/Program Files'
//
// Concatenating 'C:/Users/Public/Documents/' and 'C:/Users/User1/Documents/Financial/'
// Path.Join: 'C:/Users/Public/Documents/C:/Users/User1/Documents/Financial/'
// Path.Combine: 'C:/Users/User1/Documents/Financial/'
Imports System.IO
Module Example
Public Sub Main()
Dim path1 = "C:/Program Files/"
Dim path2 = "Utilities/SystemUtilities"
ShowPathInformation(path1, path2)
path1 = "C:/"
path2 = "/Program Files"
ShowPathInformation(path1, path2)
path1 = "C:/Users/Public/Documents/"
path2 = "C:/Users/User1/Documents/Financial/"
ShowPathInformation(path1, path2)
End Sub
Private Sub ShowPathInformation(path1 As String, path2 As String)
Dim result = Path.Join(path1.AsSpan(), path2.AsSpan())
Console.WriteLine($"Concatenating '{path1}' and '{path2}'")
Console.WriteLine($" Path.Join: '{result}'")
Console.WriteLine($" Path.Combine: '{Path.Combine(path1, path2)}'")
Console.WriteLine()
End Sub
End Module
' The example displays the following output if run on a Windows system:
' Concatenating 'C:/Program Files/' and 'Utilities/SystemUtilities'
' Path.Join: 'C:/Program Files/Utilities/SystemUtilities'
' Path.Combine: 'C:/Program Files/Utilities/SystemUtilities'
'
' Concatenating 'C:/' and '/Program Files'
' Path.Join: 'C:'Program Files'
' Path.Combine: '/Program Files'
'
' Concatenating 'C:/Users/Public/Documents/' and 'C:/Users/User1/Documents/Financial/'
' Path.Join: 'C:/Users/Public/Documents/C:/Users/User1/Documents/Financial/'
' Path.Combine: 'C:/Users/User1/Documents/Financial/'
注釈
このメソッドは、とを連結するだけで、 path
path2
2 つのパスコンポーネントがの末尾または先頭にまだ存在しない場合は、そのディレクトリの区切り文字を追加し path1
path2
ます。This method simply concatenates path
and path2
and adds a directory separator character between the two path components if one is not already present at the end of path1
or the beginning of path2
. Lengthまたはのいずれ path1
か path2
がゼロの場合、メソッドはもう一方のパスを返します。If the Length of either path1
or path2
is zero, the method returns the other path. Lengthとの両方 path1
がゼロの場合 path2
、メソッドはを返し String.Empty ます。If the Length of both path1
and path2
is zero, the method returns String.Empty.
が path1
ターゲットプラットフォームに適していないパス区切り文字で終了した場合、 Join
メソッドは元のパス区切り文字を保持し、サポートされているものを追加します。If path1
ends in a path separator character that is not appropriate for the target platform, the Join
method preserves the original path separator character and appends the supported one. この問題 \ は、Unix ベースのシステムでパスの区切りとして認識されない Windows の円記号 ("") 文字を使用するハードコードされたパスで発生します。This issue arises in hard-coded paths that use the Windows backslash ("\") character, which is not recognized as a path separator on Unix-based systems. この問題を回避するには、次の操作を行います。To work around this issue, you can:
Path.DirectorySeparatorCharディレクトリの区切り文字をハードコーディングするのではなく、プロパティの値を取得します。Retrieve the value of the Path.DirectorySeparatorChar property rather than hard-coding a directory separator character.
ディレクトリの区切り記号としてスラッシュ ("/") を使用します。Use a forward slash ("/") as the directory separator character. この文字は、 Path.DirectorySeparatorChar Unix ベースのシステムのプロパティと Windows システムのプロパティによって返され Path.AltDirectorySeparatorChar ます。This character is returned by the Path.DirectorySeparatorChar property on Unix-based systems and by the Path.AltDirectorySeparatorChar property on Windows systems.
メソッドとは異なり Combine 、 Join メソッドは返されたパスのルートを試行しません。Unlike the Combine method, the Join method does not attempt to root the returned path. (つまり、が絶対パスの場合、メソッドはを path2
Join
破棄 path1
してを返しません path2
Combine 。次の例は、2つのメソッドによって返されるパスの違いを示しています。(That is, if path2
is an absolute path, the Join
method does not discard path1
and return path2
as the Combine method does.) The following example illustrates the difference in the paths returned by the two methods. のソース path2
がユーザー入力の場合、 Combine このメソッドを使用すると、ユーザーは、アプリケーションがアクセスできないようにしたファイルシステムリソース (例では、 C:/Users/User1/Documents/財務 など) にアクセスできます。If the source of path2
is user input, the Combine method makes it possible for a user to access a file system resource (such as C:/Users/User1/Documents/Financial/ in the case of the example) that the application did not intend to make accessible.
Join
検索ワイルドカード文字にはこれらの文字を使用できるため、ディレクトリ名とファイル名に無効な文字が含まれることはありません。Not all invalid characters for directory and file names are interpreted as unacceptable by the Join
method, because you can use these characters for search wildcard characters. たとえば、 Path.Join("c:\\", "*.txt")
ファイルの作成時には無効になる場合がありますが、検索文字列として有効です。For example, while Path.Join("c:\\", "*.txt")
might be invalid when creating a file, it is valid as a search string. その Join
ため、メソッドは正常に解釈します。The Join
method therefore successfully interprets it.
こちらもご覧ください
適用対象
Join(String, String)
2 つのパスを単一のパスに連結します。Concatenates two paths into a single path.
public:
static System::String ^ Join(System::String ^ path1, System::String ^ path2);
public static string? Join (string path1, string path2);
static member Join : string * string -> string
Public Shared Function Join (path1 As String, path2 As String) As String
パラメーター
- path1
- String
結合する 1 番目のパス。The first path to join.
- path2
- String
結合する 2 番目のパス。The second path to join.
戻り値
連結されたパス。The concatenated path.
注釈
このメソッドは、とを連結するだけで、 path
path2
パスコンポーネントがまだ存在しない場合は、そのディレクトリの区切り文字を追加します。This method simply concatenates path
and path2
and adds a directory separator character between any of the path components if one is not already present. path1
またはの長さ path2
が0の場合、メソッドは残りの引数を連結します。If the length of either path1
or path2
is zero, the method concatenates the remaining argument. 結果として連結された文字列の長さがゼロの場合、メソッドはを返し String.Empty ます。If the length of the resulting concatenated string is zero, the method returns String.Empty.
が path1
ターゲットプラットフォームに適していないパス区切り文字で終了した場合、 Join
メソッドは元のパス区切り文字を保持し、サポートされているものを追加します。If path1
ends in a path separator character that is not appropriate for the target platform, the Join
method preserves the original path separator character and appends the supported one. この問題 \ は、Unix ベースのシステムでパスの区切りとして認識されない Windows の円記号 ("") 文字を使用するハードコードされたパスで発生します。This issue arises in hard-coded paths that use the Windows backslash ("\") character, which is not recognized as a path separator on Unix-based systems. この問題を回避するには、次の操作を行います。To work around this issue, you can:
Path.DirectorySeparatorCharディレクトリの区切り文字をハードコーディングするのではなく、プロパティの値を取得します。Retrieve the value of the Path.DirectorySeparatorChar property rather than hard-coding a directory separator character.
ディレクトリの区切り記号としてスラッシュ ("/") を使用します。Use a forward slash ("/") as the directory separator character. この文字は、 Path.DirectorySeparatorChar Unix ベースのシステムのプロパティと Windows システムのプロパティによって返され Path.AltDirectorySeparatorChar ます。This character is returned by the Path.DirectorySeparatorChar property on Unix-based systems and by the Path.AltDirectorySeparatorChar property on Windows systems.
メソッドとは異なり Combine 、 Join メソッドは返されたパスのルートを試行しません。Unlike the Combine method, the Join method does not attempt to root the returned path. (つまり、が絶対パスの場合、メソッドは、 path2
Join
メソッドと同じ前のパスを破棄しません Combine 。(That is, if path2
is an absolute path, the Join
method does not discard the previous paths as the Combine method does.
Join
検索ワイルドカード文字にはこれらの文字を使用できるため、ディレクトリ名とファイル名に無効な文字が含まれることはありません。Not all invalid characters for directory and file names are interpreted as unacceptable by the Join
method, because you can use these characters for search wildcard characters. たとえば、 Path.Join("c:\\", "temp", "*.txt")
ファイルの作成時には無効になる場合がありますが、検索文字列として有効です。For example, while Path.Join("c:\\", "temp", "*.txt")
might be invalid when creating a file, it is valid as a search string. その Join
ため、メソッドは正常に解釈します。The Join
method therefore successfully interprets it.
適用対象
Join(String[])
パスの配列を連結して 1 つのパスにします。Concatenates an array of paths into a single path.
public:
static System::String ^ Join(... cli::array <System::String ^> ^ paths);
public static string Join (params string[]? paths);
static member Join : string[] -> string
Public Shared Function Join (ParamArray paths As String()) As String
パラメーター
- paths
- String[]
パスの配列。An array of paths.
戻り値
連結されたパス。The concatenated path.
注釈
このメソッドは、内のすべての文字列を連結するだけで、 paths
パスコンポーネントがまだ存在しない場合は、そのディレクトリの区切り文字を追加します。This method simply concatenates all the strings in paths
and adds a directory separator character between any of the path components if one is not already present. 内の Length いずれかのパスのが paths
ゼロの場合、メソッドは残りの引数を連結します。If the Length of any of the paths in paths
is zero, the method concatenates the remaining arguments. 結果の連結された文字列の長さが0の場合、メソッドはを返し String.Empty ます。If the resulting concatenated string's length is zero, the method returns String.Empty.
のいずれかのパス (最後のパスを除く) が、 paths
ターゲットプラットフォームに適していないパス区切り文字で終了した場合、 Join
メソッドは元のパス区切り文字を保持し、サポートされているものを追加します。If any of the paths in paths
, except for the last one, ends in a path separator character that is not appropriate for the target platform, the Join
method preserves the original path separator character and appends the supported one. この問題 \ は、Unix ベースのシステムでパスの区切りとして認識されない Windows の円記号 ("") 文字を使用するハードコードされたパスで発生します。This issue arises in hard-coded paths that use the Windows backslash ("\") character, which is not recognized as a path separator on Unix-based systems. この問題を回避するには、次の操作を行います。To work around this issue, you can:
Path.DirectorySeparatorCharディレクトリの区切り文字をハードコーディングするのではなく、プロパティの値を取得します。Retrieve the value of the Path.DirectorySeparatorChar property rather than hard-coding a directory separator character.
ディレクトリの区切り記号としてスラッシュ ("/") を使用します。Use a forward slash ("/") as the directory separator character. この文字は、 Path.DirectorySeparatorChar Unix ベースのシステムのプロパティと Windows システムのプロパティによって返され Path.AltDirectorySeparatorChar ます。This character is returned by the Path.DirectorySeparatorChar property on Unix-based systems and by the Path.AltDirectorySeparatorChar property on Windows systems.
メソッドとは異なり Combine 、 Join メソッドは返されたパスのルートを試行しません。Unlike the Combine method, the Join method does not attempt to root the returned path. (つまり、最後のパスを除き、のパスのいずれかが絶対パスである場合、メソッドは、 paths
メソッドが行う Join
ように前のパスを破棄しません Combine 。(That is, if any of the paths in paths
, except for the last one, is an absolute path, the Join
method does not discard the previous paths as the Combine method does.
Join
検索ワイルドカード文字にはこれらの文字を使用できるため、ディレクトリ名とファイル名に無効な文字が含まれることはありません。Not all invalid characters for directory and file names are interpreted as unacceptable by the Join
method, because you can use these characters for search wildcard characters. たとえば、 Path.Join("c:\\", "temp", "*.txt")
ファイルの作成時には無効になる場合がありますが、検索文字列として有効です。For example, while Path.Join("c:\\", "temp", "*.txt")
might be invalid when creating a file, it is valid as a search string. その Join
ため、メソッドは正常に解釈します。The Join
method therefore successfully interprets it.
適用対象
Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)
3 つのパス コンポーネントを単一のパスに連結します。Concatenates three path components into a single path.
public:
static System::String ^ Join(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2, ReadOnlySpan<char> path3);
public static string? Join (ReadOnlySpan<char> path1, ReadOnlySpan<char> path2, ReadOnlySpan<char> path3);
public static string Join (ReadOnlySpan<char> path1, ReadOnlySpan<char> path2, ReadOnlySpan<char> path3);
static member Join : ReadOnlySpan<char> * ReadOnlySpan<char> * ReadOnlySpan<char> -> string
Public Shared Function Join (path1 As ReadOnlySpan(Of Char), path2 As ReadOnlySpan(Of Char), path3 As ReadOnlySpan(Of Char)) As String
パラメーター
- path1
- ReadOnlySpan<Char>
結合する最初のパスを含む文字範囲。A character span that contains the first path to join.
- path2
- ReadOnlySpan<Char>
結合する 2 番目のパスを含む文字範囲。A character span that contains the second path to join.
- path3
- ReadOnlySpan<Char>
結合する 3 番目のパスを含む文字範囲。A character span that contains the third path to join.
戻り値
連結されたパス。The concatenated path.
例
次の例は、メソッドとメソッドによって返されるパスの違いを示してい Path.Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>) Path.Combine(String, String, String) ます。The following example illustrates the difference in the paths returned by the Path.Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>) and Path.Combine(String, String, String) methods. 最初の文字列がドライブおよびルートディレクトリを含む完全修飾パスで、2番目の文字列が最初のパスからの相対パスである場合、2つのメソッドは同じ結果を生成します。When the first string is a fully qualified path that includes a drive and root directory and the second is a relative path from the first path, the two methods produce identical results. 2回目と3回目のメソッドの呼び出しでは、 ShowPathInformation
2 つのメソッドによって返される文字列は分岐します。In the second and third calls to the ShowPathInformation
method, the strings returned by the two methods diverge. 2番目のメソッド呼び出しでは、最初の文字列引数はドライブ、2番目の文字列はルート化されたディレクトリです。In the second method call, the first string argument is a drive, while the second is a rooted directory. メソッドは、 Join
2 つの文字列を連結し、重複するパス区切り記号を保持します。The Join
method concatenates the two strings and preserves duplicate path separators. メソッドを呼び出すと、 GetFullPath 重複が除去されます。A call to the GetFullPath method would eliminate the duplication. メソッドは、 Combine
ドライブを破棄し、現在のドライブ上のルート化されたディレクトリを返します。The Combine
method abandons the drive and returns a rooted directory on the current drive. アプリケーションの現在のドライブが C:\ の場合また、この文字列は、ディレクトリ内のファイルにアクセスするために使用され、d: ではなく C: にアクセスします。If the application's current drive is C:\ and the string is used to access a file or files in the directory, it would access C: instead of D:. 最後に、の3番目の呼び出しの最後の引数がルートになっているため ShowPathInformation
、 Join
メソッドは単にそれを最初の2つの引数に追加して無意味ファイルパスを作成 Combine
します。メソッドは、最初の2つの文字列を破棄し、3番目の文字列を返します。Finally, because the final argument in the third call to ShowPathInformation
are rooted, the Join
method simply appends it to the first two arguments to create a nonsensical file path, while the Combine
method discards the first two strings and returns the third. ファイルアクセスにこの文字列を使用すると、アプリケーションが機密ファイルに意図せずアクセスする可能性があります。Using this string for file access could give the application unintended access to sensitive files.
using System;
using System.IO;
class Program
{
static void Main()
{
string path1 = "C:/";
string path2 = "users/user1/documents";
string path3 = "letters";
ShowPathInformation(path1, path2, path3);
path1 = "D:/";
path2 = "/users/user1/documents";
path3 = "letters";
ShowPathInformation(path1, path2, path3);
path1 = "D:/";
path2 = "users/user1/documents";
path3 = "C:/users/user1/documents/data";
ShowPathInformation(path1, path2, path3);
}
private static void ShowPathInformation(string path1, string path2, string path3)
{
var result = Path.Join(path1.AsSpan(), path2.AsSpan(), path3.AsSpan());
Console.WriteLine($"Concatenating '{path1}, '{path2}', and `{path3}'");
Console.WriteLine($" Path.Join: '{result}'");
Console.WriteLine($" Path.Combine: '{Path.Combine(path1, path2, path3)}'");
Console.WriteLine($" {Path.GetFullPath(result)}");
}
}
// The example displays the following output if run on a Windows system:
// Concatenating 'C:/, 'users/user1/documents', and `letters'
// Path.Join: 'C:/users/user1/documents\letters'
// Path.Combine: 'C:/users/user1/documents\letters'
//
// Concatenating 'D:/, '/users/user1/documents', and `letters'
// Path.Join: 'D://users/user1/documents\letters'
// Path.Combine: '/users/user1/documents\letters'
//
// Concatenating 'D:/, 'users/user1/documents', and `C:/users/user1/documents/data'
// Path.Join: 'D:/users/user1/documents\C:/users/user1/documents/data'
// Path.Combine: 'C:/users/user1/documents/data'
Imports System.IO
Module Program
Public Sub Main()
Dim path1 As String = "C:/"
Dim path2 As String = "users/user1/documents"
Dim path3 As String = "letters"
ShowPathInformation(path1, path2, path3)
path1 = "D:/"
path2 = "/users/user1/documents"
path3 = "letters"
ShowPathInformation(path1, path2, path3)
path1 = "D:/"
path2 = "users/user1/documents"
path3 = "C:/users/user1/documents/data"
ShowPathInformation(path1, path2, path3)
End Sub
Private Sub ShowPathInformation(path1 As String, path2 As String, path3 As String)
Dim result = Path.Join(path1.AsSpan(), path2.AsSpan(), path3.AsSpan())
Console.WriteLine($"Concatenating '{path1}, '{path2}', and `{path3}'")
Console.WriteLine($" Path.Join: '{result}'")
Console.WriteLine($" Path.Combine: '{Path.Combine(path1, path2, path3)}'")
End Sub
End Module
' The example displays the following output if run on a Windows system:
' Concatenating 'C:/, 'users/user1/documents', and `letters'
' Path.Join: 'C:/users/user1/documents\letters'
' Path.Combine: 'C:/users/user1/documents\letters'
'
' Concatenating 'D:/, '/users/user1/documents', and `letters'
' Path.Join: 'D:'users/user1/documents\letters'
' Path.Combine: '/users/user1/documents\letters'
'
' Concatenating 'D:/, 'users/user1/documents', and `C:/users/user1/documents/data'
' Path.Join: 'D:/users/user1/documents\C:/users/user1/documents/data'
' Path.Combine: 'C:/users/user1/documents/data'
注釈
このメソッドは、、、およびを連結するだけで、 path
path2
path3
パスコンポーネントがまだ存在しない場合は、そのディレクトリの区切り文字を追加します。This method simply concatenates path
, path2
, and path3
and adds a directory separator character between any of the path components if one is not already present. Length、、またはのいずれかの引数のが0の場合 path1
path2
path3
、メソッドは残りの引数を連結します。If the Length of any of path1
, path2
, or path3
arguments is zero, the method concatenates the remaining arguments. ReadOnlySpan<T>.Lengthすべてのコンポーネントのがゼロの場合、メソッドはを返し String.Empty ます。If the ReadOnlySpan<T>.Length of all components is zero, the method returns String.Empty.
path1
またはが path2
、ターゲットプラットフォームに適していないパスの区切り文字で終わった場合、 Join
メソッドは元のパス区切り文字を保持し、サポートされているものを追加します。If path1
or path2
ends in a path separator character that is not appropriate for the target platform, the Join
method preserves the original path separator character and appends the supported one. この問題 \ は、Unix ベースのシステムでパスの区切りとして認識されない Windows の円記号 ("") 文字を使用するハードコードされたパスで発生します。This issue arises in hard-coded paths that use the Windows backslash ("\") character, which is not recognized as a path separator on Unix-based systems. この問題を回避するには、次の操作を行います。To work around this issue, you can:
Path.DirectorySeparatorCharディレクトリの区切り文字をハードコーディングするのではなく、プロパティの値を取得します。Retrieve the value of the Path.DirectorySeparatorChar property rather than hard-coding a directory separator character.
ディレクトリの区切り記号としてスラッシュ ("/") を使用します。Use a forward slash ("/") as the directory separator character. この文字は、 Path.DirectorySeparatorChar Unix ベースのシステムのプロパティと Windows システムのプロパティによって返され Path.AltDirectorySeparatorChar ます。This character is returned by the Path.DirectorySeparatorChar property on Unix-based systems and by the Path.AltDirectorySeparatorChar property on Windows systems.
メソッドとは異なり Combine 、 Join メソッドは返されたパスのルートを試行しません。Unlike the Combine method, the Join method does not attempt to root the returned path. (またはが絶対パスの場合、メソッドは、 path2
path2
メソッドが Join
行ったように、前のパスを破棄しません Combine 。(That is, if path2
or path2
is an absolute path, the Join
method does not discard the previous paths as the Combine method does.
Join
検索ワイルドカード文字にはこれらの文字を使用できるため、ディレクトリ名とファイル名に無効な文字が含まれることはありません。Not all invalid characters for directory and file names are interpreted as unacceptable by the Join
method, because you can use these characters for search wildcard characters. たとえば、 Path.Join("c:\\", "temp", "*.txt")
ファイルの作成時には無効になる場合がありますが、検索文字列として有効です。For example, while Path.Join("c:\\", "temp", "*.txt")
might be invalid when creating a file, it is valid as a search string. その Join
ため、メソッドは正常に解釈します。The Join
method therefore successfully interprets it.