Edit

Share via


Path.Join Method

Definition

Overloads

Join(String, String, String, String)

Concatenates four paths into a single path.

Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

Concatenates four path components into a single path.

Join(String, String, String)

Concatenates three paths into a single path.

Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

Concatenates three path components into a single path.

Join(ReadOnlySpan<String>)

Concatenates a span of paths into a single path.

Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>)

Concatenates two path components into a single path.

Join(String[])

Concatenates an array of paths into a single path.

Join(String, String)

Concatenates two paths into a single path.

Join(String, String, String, String)

Source:
Path.cs
Source:
Path.cs
Source:
Path.cs

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

Parameters

path1
String

The first path to join.

path2
String

The second path to join.

path3
String

The third path to join.

path4
String

The fourth path to join.

Returns

The concatenated path.

Remarks

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. If the length of any of path1, path2, path3 or path4 argument is zero, the method concatenates the remaining arguments. If the length of the resulting concatenated string is zero, the method returns String.Empty.

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. 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:

Unlike the Combine method, the Join method does not attempt to root the returned path. (That is, if path2 or path3 or path4 is an absolute path, the Join method does not discard the previous paths as the Combine method does.)

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. For example, while Path.Join("c:\\", "temp", "*.txt") might be invalid when creating a file, it is valid as a search string. The Join method therefore successfully interprets it.

Applies to

Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

Source:
Path.cs
Source:
Path.cs
Source:
Path.cs

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

Parameters

path1
ReadOnlySpan<Char>

A character span that contains the first path to join.

path2
ReadOnlySpan<Char>

A character span that contains the second path to join.

path3
ReadOnlySpan<Char>

A character span that contains the third path to join.

path4
ReadOnlySpan<Char>

A character span that contains the fourth path to join.

Returns

The concatenated path.

Remarks

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. If the Length of any of path1, path2, path3 or path4 argument is zero, the method concatenates the remaining arguments. If the ReadOnlySpan<T>.Length of all components is zero, the method returns String.Empty.

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. 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:

Unlike the Combine method, the Join method does not attempt to root the returned path. (That is, if path2 or path3 or path4 is an absolute path, the Join method does not discard the previous paths as the Combine method does.)

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. For example, while Path.Join("c:\\", "temp", "*.txt") might be invalid when creating a file, it is valid as a search string. The Join method therefore successfully interprets it.

Applies to

Join(String, String, String)

Source:
Path.cs
Source:
Path.cs
Source:
Path.cs

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

Parameters

path1
String

The first path to join.

path2
String

The second path to join.

path3
String

The third path to join.

Returns

The concatenated path.

Remarks

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. If the length of any of path1, path2 or path3 argument is zero, the method concatenates the remaining arguments. If the length of the resulting concatenated string is zero, the method returns String.Empty.

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. 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:

Unlike the Combine method, the Join method does not attempt to root the returned path. (That is, if path2 or path3 is an absolute path, the Join method does not discard the previous paths as the Combine method does.)

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. For example, while Path.Join("c:\\", "temp", "*.txt") might be invalid when creating a file, it is valid as a search string. The Join method therefore successfully interprets it.

Applies to

Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>)

Source:
Path.cs
Source:
Path.cs
Source:
Path.cs

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);
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

Parameters

path1
ReadOnlySpan<Char>

A character span that contains the first path to join.

path2
ReadOnlySpan<Char>

A character span that contains the second path to join.

path3
ReadOnlySpan<Char>

A character span that contains the third path to join.

Returns

The concatenated path.

Examples

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. 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. In the second and third calls to the ShowPathInformation method, the strings returned by the two methods diverge. In the second method call, the first string argument is a drive, while the second is a rooted directory. The Join method concatenates the two strings and preserves duplicate path separators. A call to the GetFullPath method would eliminate the duplication. The Combine method abandons the drive and returns a rooted directory on the current drive. 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:. 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 Program3
{
    static void Main()
    {
        ShowPathInformation("C:/", "users/user1/documents", "letters");
        ShowPathInformation("D:/", "/users/user1/documents", "letters");
        ShowPathInformation("D:/", "users/user1/documents", "C:/users/user1/documents/data");
    }

   private static void ShowPathInformation(string path1, string path2, string path3)
    {
        Console.WriteLine($"Concatenating  '{path1}', '{path2}', and '{path3}'");
        Console.WriteLine($"   Path.Join:     '{Path.Join(path1, path2, path3)}'");
        Console.WriteLine($"   Path.Combine:  '{Path.Combine(path1, path2, path3)}'");
        Console.WriteLine($"   {Path.GetFullPath(Path.Join(path1, path2, path3))}");
    }
}
// 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'
//    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'
//    D:\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'
//    D:\users\user1\documents\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'

Remarks

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. If the Length of any of path1, path2, or path3 arguments is zero, the method concatenates the remaining arguments. If the ReadOnlySpan<T>.Length of all components is zero, the method returns String.Empty.

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. 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:

Unlike the Combine method, the Join method does not attempt to root the returned path. (That is, if path2 or path3 is an absolute path, the Join method does not discard the previous paths as the Combine method does.)

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. For example, while Path.Join("c:\\", "temp", "*.txt") might be invalid when creating a file, it is valid as a search string. The Join method therefore successfully interprets it.

See also

Applies to

Join(ReadOnlySpan<String>)

Concatenates a span of paths into a single path.

public:
 static System::String ^ Join(ReadOnlySpan<System::String ^> paths);
public static string Join (scoped ReadOnlySpan<string?> paths);
static member Join : ReadOnlySpan<string> -> string
Public Shared Function Join (paths As ReadOnlySpan(Of String)) As String

Parameters

paths
ReadOnlySpan<String>

A span of paths.

Returns

The concatenated path.

Applies to

Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>)

Source:
Path.cs
Source:
Path.cs
Source:
Path.cs

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);
static member Join : ReadOnlySpan<char> * ReadOnlySpan<char> -> string
Public Shared Function Join (path1 As ReadOnlySpan(Of Char), path2 As ReadOnlySpan(Of Char)) As String

Parameters

path1
ReadOnlySpan<Char>

A character span that contains the first path to join.

path2
ReadOnlySpan<Char>

A character span that contains the second path to join.

Returns

The combined paths.

Examples

The following example illustrates the difference in the paths returned by the Path.Join(ReadOnlySpan<Char>, ReadOnlySpan<Char>) and Path.Combine(String, String) methods. 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. In the second and third calls to the ShowPathInformation method, the strings returned by the two methods diverge. In the second method call, the first string argument is a drive, while the second is a rooted directory. The Join method concatenates the two strings and preserves duplicate path separators. The Combine method abandons the drive and returns a rooted directory on the current drive. 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:. 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 Program2
{
    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/'

Remarks

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. If the Length of either path1 or path2 is zero, the method returns the other path. If the Length of both path1 and path2 is zero, the method returns String.Empty.

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. 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:

Unlike the Combine method, the Join method does not attempt to root the returned path. (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. 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.

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. For example, while Path.Join("c:\\", "*.txt") might be invalid when creating a file, it is valid as a search string. The Join method therefore successfully interprets it.

See also

Applies to

Join(String[])

Source:
Path.cs
Source:
Path.cs
Source:
Path.cs

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

Parameters

paths
String[]

An array of paths.

Returns

The concatenated path.

Remarks

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. If the Length of any of the paths in paths is zero, the method concatenates the remaining arguments. If the resulting concatenated string's length is zero, the method returns String.Empty.

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. 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:

Unlike the Combine method, the Join method does not attempt to root the returned path. (That is, if any of the paths in paths, except for the first one, is an absolute path, the Join method does not discard the previous paths as the Combine method does.)

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. For example, while Path.Join("c:\\", "temp", "*.txt") might be invalid when creating a file, it is valid as a search string. The Join method therefore successfully interprets it.

Applies to

Join(String, String)

Source:
Path.cs
Source:
Path.cs
Source:
Path.cs

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

Parameters

path1
String

The first path to join.

path2
String

The second path to join.

Returns

The concatenated path.

Remarks

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. If the length of either path1 or path2 is zero, the method concatenates the remaining argument. If the length of the resulting concatenated string is zero, the method returns String.Empty.

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. 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:

Unlike the Combine method, the Join method does not attempt to root the returned path. (That is, if path2 is an absolute path, the Join method does not discard the previous paths as the Combine method does.)

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. For example, while Path.Join("c:\\", "temp", "*.txt") might be invalid when creating a file, it is valid as a search string. The Join method therefore successfully interprets it.

Applies to