A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://learn.microsoft.com/en-us/dotnet/api/system.io.path.join below:

Path.Join Method (System.IO) | Microsoft Learn

Source:
Path.cs
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
path2
ReadOnlySpan<Char>

A character span that contains the second 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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4