[Tips][パス操作] 複数の文字列を1つのパスに結合する

スポンサーリンク

PathクラスCombineメソッドを使用すると、複数の文字列を連結して1つのパス文字列を作成することができます。

Combineメソッドには下記に示すように複数のオーバーロードがあります。

メソッド名 説明
Combine(String[]) 文字配列を連結してパス文字列を作成する
Combine(String, String) 2つの文字列を連結してパス文字列を作成する
Combine(String, String, String) 3つの文字列を連結してパス文字列を作成する
Combine(String, String, String, String) 4つの文字列を連結してパス文字列を作成する

文字配列からパス文字列を作成する例(VB)

Dim strText1 As String = "C:\Work"
Dim strText2 As String = "Test.txt"

'文字列を結合してパス文字列を作成する
Dim combinedPath As String = IO.Path.Combine(strText1, strText2)

MessageBox.Show(combinedPath)

文字配列からパス文字列を作成する例(C#)

string strText1 = @"C:\Work";
string strText2 ="Test.txt";

// 文字列を結合してパス文字列を作成する
string combinedPath = System.IO.Path.Combine(strText1, strText2);

MessageBox.Show(combinedPath);

複数の文字列からパス文字列を作成する(VB)

'配列を結合してパス文字列を作成する例
Dim strText() As String = {"C:\Work", "Test.txt"}
'配列を結合してパス文字列を作成する
Dim combinedPath2 As String = IO.Path.Combine(strText)

MessageBox.Show(combinedPath2)

複数の文字列からパス文字列を作成する(C#)

// 配列を結合してパス文字列を作成する例
string[] strText = {@"C:\Work", "Test.txt"};
// 配列を結合してパス文字列を作成する
string combinedPath2 = System.IO.Path.Combine(strText);

MessageBox.Show(combinedPath2);
Please follow and like us:

コメント

タイトルとURLをコピーしました