2009-06-07 12 views
79

C# içindeki iki dosya yoluna nasıl katılırım?C# 'daki iki yola nasıl katılırım?

+6

İki yolu birleştirerek ne demek istiyorsunuz? iki parça veya iki farklı dosyada dosya yolu? iki yol dosya yolunda ise System.IO.Path.Combine (yol1, yol2) kullanın. daha fazla bilgi burada [http://msdn.microsoft.com/en-us/library/system.io.path.combine.aspx] – TheVillageIdiot

cevap

129

Aşağıdaki örnekte olduğu gibi Path.Combine() kullanmak zorunda:

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

"filePath" mutlak bir yol içeriyorsa, Path.Combine sadece geri dönüyor " dosya yolu". 'string basePath = @" c: \ temp \ "; string filePath = @ "c: \ dev \ test.txt";/* neden olursa olsun */ string kombine = Path.Combine (basePath, filePath); üretir "c: \ dev \ test.txt" –

İlgili konular