How do I join two file paths in C#?
Combine() is what you need. Show activity on this post. 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.
Combines two strings into a path. Combine(String, String, String) Combines three strings into a path. Combine(String, String, String, String) Combines four strings into a path.
The path. join() method joins the specified path segments into one path. You can specify as many path segments as you like. The specified path segments must be strings, separated by comma.
C# Path filename and extensionThe Path. GetFileName returns the file name and extension of a file path represented by a read-only character span. The Path. GetFileNameWithoutExtension returns the file name without the extension of a file path represented by a read-only character span.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With