117

How do I join two file paths in C#?

1

3 Answers 3

180

You have to use Path.Combine() as in the example below:

string basePath = @"c:\temp";
string filePath = "test.txt";
string combinedPath = Path.Combine(basePath, filePath); 
// produces c:\temp\test.txt
Sign up to request clarification or add additional context in comments.

1 Comment

Its worth noting that if "filePath" contains an absolute path, Path.Combine returns only "filePath". string basePath = @"c:\temp\"; string filePath = @"c:\dev\test.txt"; /* for whatever reason */ string combined = Path.Combine(basePath, filePath); produces @"c:\dev\test.txt"
38

System.IO.Path.Combine() is what you need.

Path.Combine(path1, path2);

Comments

8

Path.Join Method

Join(String, String)

code from https://learn.microsoft.com/en-us/dotnet/api/system.io.path.join?view=net-5.0#System_IO_Path_Join_System_String_System_String_

class Program
{
    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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.