2009-06-07 18 views
79

Come faccio a unire due percorsi di file in C#?Come posso unire due percorsi in C#?

+6

cosa intendi per unire due percorsi? percorso del file in due parti o due file diversi? se il percorso del file è diviso in due parti, utilizzare System.IO.Path.Combine (path1, path2). maggiori informazioni qui [http://msdn.microsoft.com/en-us/library/system.io.path.combine.aspx] – TheVillageIdiot

risposta

129

Devi usare Path.Combine() come nell'esempio qui sotto:

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

Vale la pena notare che se "filePath" contiene un percorso assoluto, Path.Combine restituisce solo " percorso del file". 'string basePath = @" c: \ temp \ "; string filePath = @ "c: \ dev \ test.txt";/* per qualsiasi motivo */ stringa combinata = Path.Combine (basePath, filePath); ' produce @" c: \ dev \ test.txt " –