2009-06-07 4 views
79

C#で2つのファイルパスを結合するにはどうすればよいですか?C#で2つのパスを結合するにはどうすればよいですか?

+6

2つのパスを結合することは何を意味しますか? 2つの部分または2つの異なるファイルのファイルパス?ファイルのパスが2つの部分ではSystem.IO.Path.Combine(path1、path2)を使用します。 more info here [http://msdn.microsoft.com/en-us/library/system.io.path.combine.aspx] – TheVillageIdiot

答えて

129

あなたは、以下の例のようにPath.Combine()を使用する必要があります。

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

"filePath"に絶対パスが含まれている場合、Path.Combineは "ファイルパス"。 'string basePath = @" c:\ temp \ "; 文字列filePath = @ "c:\ dev \ test.txt";/*理由を問わず*/ string combined = Path.Combine(basePath、filePath); ' 'は@ "c:\ dev \ test.txt"を生成します。 –

関連する問題