Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I join two paths in C#?

Tags:

c#

file

path

How do I join two file paths in C#?

like image 522
Geo Avatar asked Jun 07 '09 11:06

Geo


People also ask

How to Combine 2 path 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.

What is path combine?

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.

What is the purpose path join?

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.

How define file path in C#?

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.


1 Answers

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 
like image 175
Jose Basilio Avatar answered Oct 31 '22 18:10

Jose Basilio