Path.Combine メソッド
アセンブリ: mscorlib (mscorlib.dll 内)

Dim path1 As String Dim path2 As String Dim returnValue As String returnValue = Path.Combine(path1, path2)
- path1
第 1 のパス。
- path2
第 2 のパス。
結合したパスを含む文字列。指定したパスの 1 つが長さ 0 の文字列の場合、このメソッドは別のパスを返します。path2 に絶対パスが含まれる場合、このメソッドは path2 を返します。


path1 がドライブ参照 (つまり、"C:" や "D:") ではなく、DirectorySeparatorChar、AltDirectorySeparatorChar、または VolumeSeparatorChar で定義された有効な区切り記号で終了していない場合は、連結前に DirectorySeparatorChar が path1 に追加されます。
path2 にルートが含まれていない (たとえば、path2 が区切り記号またはドライブ仕様で始まらない) 場合は、間に区切り記号が入った 2 つのパスの連結が返されます。path2 にルートが含まれている場合は、path2 が返されます。
パラメータに空白が含まれる場合は解析されないため、path2 が " c:\\ " の場合は path2 だけを返すのではなく、これが path1 に追加されます。
ディレクトリ名やファイル名内の無効な文字は、検索ワイルドカード文字として使用できるため、これらすべてが Combine メソッドで受け入れられないと解釈されるわけではありません。たとえば、Path.Combine("c:\\", "*.txt") は、ファイルを作成するときは無効ですが、検索文字列としては有効です。したがって、Combine メソッドで正常に解釈されます。
このメソッドの使用例については、以下の「使用例」を参照してください。その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。
GetExtension | |
GetFullPath | |
GetFileName | |
GetFileNameWithoutExtension | |
GetDirectoryName | |
ChangeExtension | |
GetFileSystemInfos | |

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:^*&)(_=@#'\\^.*(.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:^*&)(_=@#'\^.*(.txt' and 'subdir\file.txt', the result is: ' 'c:^*&)(_=@#'\^.*(.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:^*&)(_=@#'\\^.*(.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:^*&)(_=@#'\^.*(.txt' and 'subdir\file.txt', the result is: // 'c:^*&)(_=@#'\^.*(.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:^*&)(_=@#'\\^.*(.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:^*&)(_=@#'\\^.*(.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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からPath.Combine メソッドを検索する場合は、下記のリンクをクリックしてください。

- Path.Combine メソッドのページへのリンク