Path.Combine 메서드

정의

여러 문자열을 한 경로로 결합합니다.

오버로드

Combine(String[])

문자열 배열을 한 경로로 결합합니다.

Combine(String, String)

두 문자열을 한 경로로 결합합니다.

Combine(String, String, String)

세 문자열을 한 경로로 결합합니다.

Combine(String, String, String, String)

네 문자열을 한 경로로 결합합니다.

설명

이 메서드는 개별 문자열을 파일 경로를 나타내는 단일 문자열에 연결하기 위한 것입니다. 그러나 첫 번째 이외의 인수에 루팅된 경로가 포함된 경우 이전 경로 구성 요소는 무시되고 반환된 문자열은 해당 루트 경로 구성 요소로 시작됩니다. 메서드 대신 Combine 또는 TryJoin 메서드를 Join 사용하는 것이 좋습니다.

중요

이 메서드는 첫 번째 인수가 절대 경로이고 다음 인수 또는 인수가 상대 경로라고 가정합니다. 그렇지 않은 경우, 특히 후속 인수가 사용자가 입력한 문자열인 경우 또는 TryJoin 메서드를 대신 호출 Join 합니다.

Combine(String[])

Source:
Path.cs
Source:
Path.cs
Source:
Path.cs

문자열 배열을 한 경로로 결합합니다.

public:
 static System::String ^ Combine(... cli::array <System::String ^> ^ paths);
public static string Combine (params string[] paths);
static member Combine : string[] -> string
Public Shared Function Combine (ParamArray paths As String()) As String

매개 변수

paths
String[]

경로 각 부분의 배열입니다.

반환

결합된 경로입니다.

예외

.NET Framework 및 .NET Core 버전이 2.1보다 오래된 경우: 배열의 문자열 중 하나에 에 정의된 잘못된 문자가 하나 이상 포함되어 있습니다GetInvalidPathChars().

배열의 문자열 중 하나가 null인 경우

예제

다음 예제에서는 문자열 배열을 경로로 결합합니다.

string[] paths = {@"d:\archives", "2001", "media", "images"};
string fullPath = Path.Combine(paths);
Console.WriteLine(fullPath);
Dim paths As String() = {"d:\archives", "2001", "media", "images"}
Dim fullPath As String = Path.Combine(paths)
Console.WriteLine(fullPath)

설명

paths 는 결합할 경로 부분의 배열이어야 합니다. 후속 경로 중 하나가 절대 경로인 경우 결합 작업은 해당 절대 경로부터 다시 설정되어 이전의 결합된 모든 경로를 삭제합니다.

의 요소가 있지만 마지막 요소가 paths 드라이브가 아니고 또는 AltDirectorySeparatorChar 문자로 DirectorySeparatorChar 끝나지 않는 경우 메서드는 Combine 해당 요소와 다음 요소 사이에 문자를 추가합니다DirectorySeparatorChar. 요소가 대상 플랫폼 Combine 에 적합하지 않은 경로 구분 기호 문자로 끝나는 경우 메서드는 원래 경로 구분 기호 문자를 유지하고 지원되는 문자를 추가합니다. 다음 예제에서는 백슬래시가 경로 구분 기호 문자로 사용되는 경우 Windows 및 Unix 기반 시스템의 결과를 비교합니다.

string[] paths = {@"d:\archives", "2001", "media", "images"};
string fullPath = Path.Combine(paths);
Console.WriteLine(fullPath);            

paths = new string[] {@"d:\archives\", @"2001\", "media", "images"};
fullPath = Path.Combine(paths);
Console.WriteLine(fullPath); 

paths = new string[] {"d:/archives/", "2001/", "media", "images"};
fullPath = Path.Combine(paths);
Console.WriteLine(fullPath); 
// The example displays the following output if run on a Windows system:
//    d:\archives\2001\media\images
//    d:\archives\2001\media\images
//    d:/archives/2001/media\images
//
// The example displays the following output if run on a Unix-based system:
//    d:\archives/2001/media/images
//    d:\archives\/2001\/media/images
//    d:/archives/2001/media/images
Dim paths As String() = { "d:\archives", "2001", "media", "images" }
Dim fullPath As String = Path.Combine(paths)
Console.WriteLine(fullPath)            

paths = { "d:\archives\", "2001\", "media", "images" }
fullPath = Path.Combine(paths)
Console.WriteLine(fullPath) 

paths = { "d:/archives/", "2001/", "media", "images" }
fullPath = Path.Combine(paths)
Console.WriteLine(fullPath) 
' The example displays the following output if run on a Windows system:
'    d:\archives\2001\media\images
'    d:\archives\2001\media\images
'    d:/archives/2001/media\images
'
' The example displays the following output if run on a Linux system:
'    d:\archives/2001/media/images
'    d:\archives\/2001\/media/images
'    d:/archives/2001/media/images

길이가 0인 문자열은 결합된 경로에서 생략됩니다.

매개 변수는 공백이 있는 경우 구문 분석되지 않습니다.

.NET Framework 및 .NET Core 버전이 2.1보다 오래된 경우: 이러한 문자를 검색 와일드카드 문자에 사용할 수 있으므로 디렉터리 및 파일 이름에 Combine 대한 모든 잘못된 문자가 메서드에서 허용되지 않는 것으로 해석되는 것은 아닙니다. 예를 들어 파일을 Path.Combine("c:\\", "*.txt") 만들려는 경우 유효하지 않을 수 있지만 검색 문자열로 유효합니다. 따라서 메서드에 의해 Combine 성공적으로 해석됩니다.

추가 정보

적용 대상

Combine(String, String)

Source:
Path.cs
Source:
Path.cs
Source:
Path.cs

두 문자열을 한 경로로 결합합니다.

public:
 static System::String ^ Combine(System::String ^ path1, System::String ^ path2);
public static string Combine (string path1, string path2);
static member Combine : string * string -> string
Public Shared Function Combine (path1 As String, path2 As String) As String

매개 변수

path1
String

결합할 첫 번째 경로입니다.

path2
String

결합할 두 번째 경로입니다.

반환

결합된 경로입니다. 지정된 경로 중 하나가 0 길이의 문자열이면 이 메서드는 다른 경로를 반환합니다. path2에 절대 경로가 포함되어 있으면 이 메서드는 path2을(를) 반환합니다.

예외

2.1보다 오래된 .NET Framework 및 .NET Core 버전: path1 또는 path2 에 정의된 잘못된 문자가 하나 이상 포함되어 있습니다GetInvalidPathChars().

path1 또는 path2null인 경우

예제

다음 예제에서는 사용 하는 방법을 보여 줍니다는 Combine Windows에서 메서드.

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 ) 
   {
      if (p1 == nullptr)
         p1 = "nullptr";
      if (p2 == nullptr)
         p2 = "nullptr";
      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 );
}
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 = "";

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

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

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

        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'
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

    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
            If p1 = Nothing Then
                p1 = "Nothing"
            End If
            If p2 = Nothing Then
                p2 = "Nothing"
            End If
            Console.WriteLine("You cannot combine '{0}' and '{1}' because: {2}{3}", p1, p2, Environment.NewLine, e.Message)
        End Try
        
        Console.WriteLine()
    End Sub
End Class
' 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

설명

가 드라이브 참조(즉, "C:" 또는 "D:")가 아니고 , 또는 VolumeSeparatorChar에 정의된 유효한 구분 기호 문자로 끝나지 않는 경우 path1 연결 전에 에 추가됩니다path1.DirectorySeparatorCharAltDirectorySeparatorCharDirectorySeparatorChar 대상 플랫폼 Combine 에 적합하지 않은 경로 구분 기호 문자로 끝나는 경우 path1 메서드는 원래 경로 구분 기호 문자를 유지하고 지원되는 문자를 추가합니다. 다음 예제에서는 백슬래시가 경로 구분 기호 문자로 사용되는 경우 Windows 및 Unix 기반 시스템의 결과를 비교합니다.

var result = Path.Combine(@"C:\Pictures\", "Saved Pictures"); 
Console.WriteLine(result);
// The example displays the following output if run on a Windows system:
//    C:\Pictures\Saved Pictures
//
// The example displays the following output if run on a Unix-based system:
//    C:\Pictures\/Saved Pictures
Dim result = Path.Combine("C:\Pictures\", "Saved Pictures") 
Console.WriteLine(result)
' The example displays the following output if run on a Windows system:
'    C:\Pictures\Saved Pictures
'
' The example displays the following output if run on a Unix-based system:
'    C:\Pictures\/Saved Pictures

가 루트를 포함하지 않는 경우 path2 (예: 가 구분 기호 문자 또는 드라이브 사양으로 시작하지 않는 경우 path2 ) 결과는 중간 구분 기호 문자가 있는 두 경로의 연결입니다. 루트를 포함하는 경우 path2path2 반환됩니다.

매개 변수는 공백이 있는 경우 구문 분석되지 않습니다. 따라서 공백(예: " \file.txt ")이 포함된 경우 path2 메서드는 Combinepath2반환하는 대신 에 를 추가합니다 path2path1.

.NET Framework 및 .NET Core 버전이 2.1보다 오래된 경우: 이러한 문자를 검색 와일드카드 문자에 사용할 수 있으므로 디렉터리 및 파일 이름에 Combine 대한 모든 잘못된 문자가 메서드에서 허용되지 않는 것으로 해석되는 것은 아닙니다. 예를 들어 파일을 Path.Combine("c:\\", "*.txt") 만들려는 경우 유효하지 않을 수 있지만 검색 문자열로 유효합니다. 따라서 메서드에 의해 Combine 성공적으로 해석됩니다.

일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.

추가 정보

적용 대상

Combine(String, String, String)

Source:
Path.cs
Source:
Path.cs
Source:
Path.cs

세 문자열을 한 경로로 결합합니다.

public:
 static System::String ^ Combine(System::String ^ path1, System::String ^ path2, System::String ^ path3);
public static string Combine (string path1, string path2, string path3);
static member Combine : string * string * string -> string
Public Shared Function Combine (path1 As String, path2 As String, path3 As String) As String

매개 변수

path1
String

결합할 첫 번째 경로입니다.

path2
String

결합할 두 번째 경로입니다.

path3
String

결합할 세 번째 경로입니다.

반환

결합된 경로입니다.

예외

2.1보다 오래된 .NET Framework 및 .NET Core 버전: path1, path2또는 path3 에 정의된 잘못된 문자가 하나 이상 포함되어 있습니다GetInvalidPathChars().

path1, path2 또는 path3null입니다.

예제

다음 예제에서는 세 가지 경로를 결합합니다.

string p1 = @"d:\archives\";
string p2 = "media";
string p3 = "images";
string combined = Path.Combine(p1, p2, p3);
Console.WriteLine(combined);
Dim p1 As String = "d:\archives\"
Dim p2 As String = "media"
Dim p3 As String = "images"
Dim combined As String = Path.Combine(p1, p2, p3)
Console.WriteLine(combined)

설명

path1 절대 경로여야 합니다(예: "d:\archives" 또는 "\\archives\public"). 또는 path3 가 절대 경로인 경우 path2 결합 작업은 이전에 결합된 모든 경로를 삭제하고 해당 절대 경로로 다시 설정합니다.

길이가 0인 문자열은 결합된 경로에서 생략됩니다.

또는 path2 가 드라이브 참조(즉, "C:" 또는 "D:")가 아니고 , AltDirectorySeparatorChar또는 DirectorySeparatorCharVolumeSeparatorCharDirectorySeparatorChar정의된 유효한 구분 기호 문자로 끝나지 않는 경우 path1 또는 연결 전에 가 추가됩니다 path1path2. 또는 path2 가 대상 플랫폼 Combine 에 적합하지 않은 경로 구분 기호 문자로 끝나는 경우 path1 메서드는 원래 경로 구분 기호 문자를 유지하고 지원되는 문자를 추가합니다. 다음 예제에서는 백슬래시가 경로 구분 기호 문자로 사용되는 경우 Windows 및 Unix 기반 시스템의 결과를 비교합니다.

var result = Path.Combine(@"C:\Pictures\", @"Saved Pictures\", "2019"); 
Console.WriteLine(result);
// The example displays the following output if run on a Windows system:
//    C:\Pictures\Saved Pictures\2019
//
// The example displays the following output if run on a Unix-based system:
//    C:\Pictures\/Saved Pictures\/2019
Dim result = Path.Combine("C:\Pictures\", "Saved Pictures\", "2019") 
Console.WriteLine(result)
' The example displays the following output if run on a Windows system:
'    C:\Pictures\Saved Pictures\2019
'
' The example displays the following output if run on a Unix-based system:
'    C:\Pictures\/Saved Pictures\/2019

가 루트를 포함하지 않는 경우 path2 (예: 가 구분 기호 문자 또는 드라이브 사양으로 시작하지 않는 경우 path2 ) 결과는 중간 구분 기호 문자가 있는 두 경로의 연결입니다. 루트를 포함하는 경우 path2path2 반환됩니다.

매개 변수는 공백이 있는 경우 구문 분석되지 않습니다. 따라서 공백이 포함된 경우path2(예: " \file.txt ") 메서드가 Combinepath1추가됩니다path2.

.NET Framework 및 .NET Core 버전이 2.1보다 오래된 경우: 이러한 문자를 검색 와일드카드 문자에 사용할 수 있으므로 디렉터리 및 파일 이름에 Combine 대한 모든 잘못된 문자가 메서드에서 허용되지 않는 것으로 해석되는 것은 아닙니다. 예를 들어 파일을 Path.Combine("c:\\", "*.txt") 만들려는 경우 유효하지 않을 수 있지만 검색 문자열로 유효합니다. 따라서 메서드에 의해 Combine 성공적으로 해석됩니다.

추가 정보

적용 대상

Combine(String, String, String, String)

Source:
Path.cs
Source:
Path.cs
Source:
Path.cs

네 문자열을 한 경로로 결합합니다.

public:
 static System::String ^ Combine(System::String ^ path1, System::String ^ path2, System::String ^ path3, System::String ^ path4);
public static string Combine (string path1, string path2, string path3, string path4);
static member Combine : string * string * string * string -> string
Public Shared Function Combine (path1 As String, path2 As String, path3 As String, path4 As String) As String

매개 변수

path1
String

결합할 첫 번째 경로입니다.

path2
String

결합할 두 번째 경로입니다.

path3
String

결합할 세 번째 경로입니다.

path4
String

결합할 네 번째 경로입니다.

반환

결합된 경로입니다.

예외

2.1보다 오래된 .NET Framework 및 .NET Core 버전: path1, path2, path3또는 path4 에 정의된 잘못된 문자가 하나 이상 포함되어 있습니다GetInvalidPathChars().

path1, path2, path3 또는 path4null입니다.

예제

다음 예제에서는 네 개의 경로를 결합합니다.

string path1 = @"d:\archives\";
string path2 = "2001";
string path3 = "media";
string path4 = "images";
string combinedPath = Path.Combine(path1, path2, path3, path4);
Console.WriteLine(combinedPath);
Dim path1 As String = "d:\archives\"
Dim path2 As String = "2001"
Dim path3 As String = "media"
Dim path4 As String = "imaged"
Dim combinedPath As String = Path.Combine(path1, path2, path3, path4)
Console.WriteLine(combined)

설명

path1 절대 경로여야 합니다(예: "d:\archives" 또는 "\\archives\public"). 후속 경로 중 하나가 절대 경로인 경우 결합 작업은 이전에 결합된 모든 경로를 삭제하고 해당 절대 경로로 다시 설정합니다.

길이가 0인 문자열은 결합된 경로에서 생략됩니다.

, path2또는 가 드라이브 참조(즉, "C:" 또는 "D:")가 아니고 , AltDirectorySeparatorChar또는 DirectorySeparatorCharVolumeSeparatorCharDirectorySeparatorChar정의된 유효한 구분 기호 문자로 끝나지 않는 경우 path1연결 전에 에 추가 path3 됩니다. , path2또는 path3 가 대상 플랫폼 Combine 에 적합하지 않은 경로 구분 기호 문자로 끝나는 경우 path1메서드는 원래 경로 구분 기호 문자를 유지하고 지원되는 문자를 추가합니다. 다음 예제에서는 백슬래시가 경로 구분 기호 문자로 사용되는 경우 Windows 및 Unix 기반 시스템의 결과를 비교합니다.

var result = Path.Combine(@"C:\Pictures\", @"Saved Pictures\", @"2019\", @"Jan\"); 
Console.WriteLine(result);
// The example displays the following output if run on a Windows system:
//    C:\Pictures\Saved Pictures\2019\Jan\
//
// The example displays the following output if run on a Unix-based system:
//    C:\Pictures\Saved Pictures\2019\Jan\
Dim result = Path.Combine("C:\Pictures\", "Saved Pictures\", "2019\", "Jan\") 
Console.WriteLine(result)
' The example displays the following output if run on a Windows system:
'    C:\Pictures\Saved Pictures\2019\Jan\
'
' The example displays the following output if run on a Unix-based system:
'    C:\Pictures\Saved Pictures\2019\Jan\

가 루트를 포함하지 않는 경우 path2 (예: 가 구분 기호 문자 또는 드라이브 사양으로 시작하지 않는 경우 path2 ) 결과는 중간 구분 기호 문자가 있는 두 경로의 연결입니다. 루트를 포함하는 경우 path2path2 반환됩니다.

매개 변수는 공백이 있는 경우 구문 분석되지 않습니다. 따라서 공백이 포함된 경우path2(예: " \file.txt ") 메서드가 Combinepath1추가됩니다path2.

.NET Framework 및 .NET Core 버전이 2.1보다 오래된 경우: 이러한 문자를 검색 와일드카드 문자에 사용할 수 있으므로 디렉터리 및 파일 이름에 Combine 대한 모든 잘못된 문자가 메서드에서 허용되지 않는 것으로 해석되는 것은 아닙니다. 예를 들어 파일을 Path.Combine("c:\\", "*.txt") 만들려는 경우 유효하지 않을 수 있지만 검색 문자열로 유효합니다. 따라서 메서드에 의해 Combine 성공적으로 해석됩니다.

추가 정보

적용 대상