Path.Combine メソッドとは? わかりやすく解説

Path.Combine メソッド

2 つパス文字列結合します

名前空間: System.IO
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Function Combine
 ( _
    path1 As String, _
    path2 As String _
) As String
Dim path1 As String
Dim path2 As String
Dim returnValue As String

returnValue = Path.Combine(path1, path2)
public:
static String^ Combine (
    String^ path1, 
    String^ path2
)

パラメータ

path1

第 1 のパス

path2

第 2 のパス

戻り値
結合したパスを含む文字列指定したパス1 つ長さ 0 の文字列場合、このメソッド別のパス返しますpath2絶対パス含まれる場合、このメソッドpath2返します

例外例外
例外種類条件

ArgumentException

path1 または path2 に、InvalidPathChars で定義されている無効な文字1 つ以上含まれているか、ワイルドカード文字含まれています。

ArgumentNullException

path1 または path2null 参照 (Visual Basic では Nothing) です。

解説解説

path1ドライブ参照 (つまり、"C:" や "D:") ではなく、DirectorySeparatorChar、AltDirectorySeparatorChar、または VolumeSeparatorChar で定義され有効な区切り記号終了してない場合は、連結前に DirectorySeparatorCharpath1追加されます。

path2ルート含まれていない (たとえば、path2区切り記号またはドライブ仕様始まらない) 場合は、間に区切り記号入った 2 つパス連結返されます。path2ルート含まれている場合は、path2返されます。

パラメータ空白含まれる場合解析されないため、path2 が " c:\\ " の場合path2 だけを返すではなく、これが path1追加されます。

ディレクトリ名やファイル名内の無効な文字は、検索ワイルドカード文字として使用できるため、これらすべてが Combine メソッド受け入れられない解釈されるわけではありません。たとえば、Path.Combine("c:\\", "*.txt") は、ファイル作成するときは無効ですが、検索文字列としては有効です。したがってCombine メソッド正常に解釈されます。

このメソッド使用例については、以下の「使用例」を参照してくださいその他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します

使用例使用例

Windows ベースデスクトップ プラットフォーム上で Combine メソッド使用するコード例次に示します

Imports System
Imports System.IO

Public Class ChangeExtensionTest
    
    
    Public Shared Sub Main()
        Dim path1 As String
 = "c:\temp"
        Dim path2 As String
 = "subdir\file.txt"
        Dim path3 As String
 = "c:\temp.txt"
        Dim path4 As String
 = "c:^*&)(_=@#'\\^&#2.*(.txt"
        Dim path5 As String
 = ""
        Dim path6 As String
 = Nothing

        CombinePaths(path1, path2)
        CombinePaths(path1, path3)
        CombinePaths(path3, path2)
        CombinePaths(path4, path2)
        CombinePaths(path5, path2)
        CombinePaths(path6, path2)
    End Sub 'Main

    Private Shared Sub CombinePaths(p1
 As String, p2 As String)
        
        Try
            Dim combination As String
 = Path.Combine(p1, p2)
            
            Console.WriteLine("When you combine '{0}' and '{1}',
 the result is: {2}'{3}'", p1, p2, Environment.NewLine, combination)
        Catch e As Exception
            Console.WriteLine("You cannot combine '{0}' and '{1}'
 because: {2}{3}", p1, p2, Environment.NewLine, e.Message)
        End Try
        
        Console.WriteLine()
    End Sub 'CombinePaths
End Class 'ChangeExtensionTest
' This code produces output similar to the following:
'
' When you combine 'c:\temp' and 'subdir\file.txt', the result is: 
' 'c:\temp\subdir\file.txt'
' 
' When you combine 'c:\temp' and 'c:\temp.txt', the result is: 
' 'c:\temp.txt'
' 
' When you combine 'c:\temp.txt' and 'subdir\file.txt', the result is:
 
' 'c:\temp.txt\subdir\file.txt'
' 
' When you combine 'c:^*&)(_=@#'\^&#2.*(.txt' and 'subdir\file.txt',
 the result is: 
' 'c:^*&)(_=@#'\^&#2.*(.txt\subdir\file.txt'
' 
' When you combine '' and 'subdir\file.txt', the result is: 
' 'subdir\file.txt'
' 
' You cannot combine '' and 'subdir\file.txt' because: 
' Value cannot be null.
' Parameter name: path1

using System;
using System.IO;

public class ChangeExtensionTest {

    public static void Main()
 {

        string path1 = "c:\\temp";
        string path2 = "subdir\\file.txt";
        string path3 = "c:\\temp.txt";
        string path4 = "c:^*&)(_=@#'\\^&#2.*(.txt";
        string path5 = "";
        string path6 = null;

        CombinePaths(path1, path2);
        CombinePaths(path1, path3);
        CombinePaths(path3, path2);
        CombinePaths(path4, path2);
        CombinePaths(path5, path2);
        CombinePaths(path6, path2);
    }

    private static void
 CombinePaths(string p1, string p2) {

        try {
            string combination = Path.Combine(p1, p2);

            Console.WriteLine("When you combine '{0}' and '{1}', the result
 is: {2}'{3}'",
                        p1, p2, Environment.NewLine, combination);
        } catch (Exception e) {
            Console.WriteLine("You cannot combine '{0}' and '{1}' because: {2}{3}"
,
                        p1, p2, Environment.NewLine, e.Message);
        }

        Console.WriteLine();
    }
}
// This code produces output similar to the following:
//
// When you combine 'c:\temp' and 'subdir\file.txt', the result is:
 
// 'c:\temp\subdir\file.txt'
// 
// When you combine 'c:\temp' and 'c:\temp.txt', the result is: 
// 'c:\temp.txt'
// 
// When you combine 'c:\temp.txt' and 'subdir\file.txt', the result
 is: 
// 'c:\temp.txt\subdir\file.txt'
// 
// When you combine 'c:^*&)(_=@#'\^&#2.*(.txt' and 'subdir\file.txt',
 the result is: 
// 'c:^*&)(_=@#'\^&#2.*(.txt\subdir\file.txt'
// 
// When you combine '' and 'subdir\file.txt', the result is: 
// 'subdir\file.txt'
// 
// You cannot combine '' and 'subdir\file.txt' because: 
// Value cannot be null.
// Parameter name: path1
using namespace System;
using namespace System::IO;
void CombinePaths( String^ p1, String^ p2 )
{
   try
   {
      String^ combination = Path::Combine( p1, p2 );
      Console::WriteLine( "When you combine '{0}' and '{1}', the result is:
 {2}'{3}'", p1, p2, Environment::NewLine, combination );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "You cannot combine '{0}' and '{1}' because: {2}{3}",
 p1, p2, Environment::NewLine, e->Message );
   }

   Console::WriteLine();
}

int main()
{
   String^ path1 = "c:\\temp";
   String^ path2 = "subdir\\file.txt";
   String^ path3 = "c:\\temp.txt";
   String^ path4 = "c:^*&)(_=@#'\\^&#2.*(.txt";
   String^ path5 = "";
   String^ path6 = nullptr;
   CombinePaths( path1, path2 );
   CombinePaths( path1, path3 );
   CombinePaths( path3, path2 );
   CombinePaths( path4, path2 );
   CombinePaths( path5, path2 );
   CombinePaths( path6, path2 );
}

import System.*;  
import System.IO.*;  

public class ChangeExtensionTest
{
    public static void main(String[]
 args)
    {
        String path1 = "c:\\temp";
        String path2 = "subdir\\file.txt";
        String path3 = "c:\\temp.txt";
        String path4 = "c:^*&)(_=@#'\\^&#2.*(.txt";
        String path5 = "";
        String path6 = null;

        CombinePaths(path1, path2);
        CombinePaths(path1, path3);
        CombinePaths(path3, path2);
        CombinePaths(path4, path2);
        CombinePaths(path5, path2);
        CombinePaths(path6, path2);
    } //main

    private static void
 CombinePaths(String p1, String p2)
    {
        try {
            String combination = Path.Combine(p1, p2);

            Console.WriteLine("When you combine '{0}' and '{1}', the result
 "
                + "is: {2}'{3}'", new Object[] { p1,
 p2, 
                Environment.get_NewLine(), combination });
        }
        catch (System.Exception e) {
            Console.WriteLine("You cannot combine '{0}' and '{1}' because: "
                + "{2}{3}", new Object[] { p1, p2, Environment.get_NewLine(),
 
                e.get_Message() });
        }
        Console.WriteLine();
    } //CombinePaths
} //ChangeExtensionTest
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からPath.Combine メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からPath.Combine メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からPath.Combine メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Path.Combine メソッド」の関連用語

Path.Combine メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Path.Combine メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS