Jak połączyć dwie ścieżki w C #?


Odpowiedzi:


158

Musisz użyć Path.Combine () jak w poniższym przykładzie:

string basePath = @"c:\temp";
string filePath = "test.txt";
string combinedPath = Path.Combine(basePath, filePath); 
// produces c:\temp\test.txt

14
Warto zauważyć, że jeśli „filePath” zawiera bezwzględną ścieżkę, Path.Combine zwraca tylko „filePath”. string basePath = @"c:\temp\"; string filePath = @"c:\dev\test.txt"; /* for whatever reason */ string combined = Path.Combine(basePath, filePath);produkuje @ "c: \ dev \ test.txt"
Jan 'splite' K.

Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.