Path.Combine Method

Definition

Combines strings into a path.

Overloads

Combine(ReadOnlySpan<String>)

Combines a span of strings into a path.

Combine(String[])

Combines an array of strings into a path.

Combine(String, String)

Combines two strings into a path.

Combine(String, String, String)

Combines three strings into a path.

Combine(String, String, String, String)

Combines four strings into a path.

Remarks

This method is intended to concatenate individual strings into a single string that represents a file path. However, if an argument other than the first contains a rooted path, any previous path components are ignored, and the returned string begins with that rooted path component. As an alternative to the Combine method, consider using the Join or TryJoin methods (not available in .NET Framework).

Important

This method assumes that the first argument is an absolute path and that the following argument or arguments are relative paths. If this is not the case, and particularly if any subsequent arguments are strings input by the user, call the Join or TryJoin method instead.

Combine(ReadOnlySpan<String>)

Combines a span of strings into a path.

public:
 static System::String ^ Combine(ReadOnlySpan<System::String ^> paths);
public static string Combine (scoped ReadOnlySpan<string> paths);
static member Combine : ReadOnlySpan<string> -> string
Public Shared Function Combine (paths As ReadOnlySpan(Of String)) As String

Parameters

paths
ReadOnlySpan<String>

A span of parts of the path.

Returns

The combined paths.

Applies to

Combine(String[])

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

Combines an array of strings into a path.

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

Parameters

paths
String[]

An array of parts of the path.

Returns

The combined paths.

Exceptions

.NET Framework and .NET Core versions older than 2.1: One of the strings in the array contains one or more of the invalid characters defined in GetInvalidPathChars().

One of the strings in the array is null.

Examples

The following example combines an array of strings into a path.

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)

Remarks

paths should be an array of the parts of the path to combine. If the one of the subsequent paths is an absolute path, then the combine operation resets starting with that absolute path, discarding all previous combined paths.

If any element in paths but the last one is not a drive and does not end with either the DirectorySeparatorChar or the AltDirectorySeparatorChar character, the Combine method adds a DirectorySeparatorChar character between that element and the next one. Note that, if the element ends in a path separator character that is not appropriate for the target platform, the Combine method preserves the original path separator character and appends a supported one. The following example compares the result on Windows and Unix-based systems when the backslash is used as a path separator character.

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

Zero-length strings are omitted from the combined path.

The parameters are not parsed if they have white space.

.NET Framework and .NET Core versions older than 2.1: Not all invalid characters for directory and file names are interpreted as unacceptable by the Combine method, because you can use these characters for search wildcard characters. For example, while Path.Combine("c:\\", "*.txt") might be invalid if you were to create a file from it, it is valid as a search string. It is therefore successfully interpreted by the Combine method.

See also

Applies to

Combine(String, String)

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

Combines two strings into a path.

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

Parameters

path1
String

The first path to combine.

path2
String

The second path to combine.

Returns

The combined paths. If one of the specified paths is a zero-length string, this method returns the other path. If path2 contains an absolute path, this method returns path2.

Exceptions

.NET Framework and .NET Core versions older than 2.1: path1 or path2 contains one or more of the invalid characters defined in GetInvalidPathChars().

path1 or path2 is null.

Examples

The following example demonstrates using the Combine method on 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

Remarks

If path1 is not a drive reference (that is, "C:" or "D:") and does not end with a valid separator character as defined in DirectorySeparatorChar, AltDirectorySeparatorChar, or VolumeSeparatorChar, DirectorySeparatorChar is appended to path1 before concatenation. Note that if path1 ends in a path separator character that is not appropriate for the target platform, the Combine method preserves the original path separator character and appends a supported one. The following example compares the result on Windows and Unix-based systems when the backslash is used as a path separator character.

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

If path2 does not include a root (for example, if path2 does not start with a separator character or a drive specification), the result is a concatenation of the two paths, with an intervening separator character. If path2 includes a root, path2 is returned.

The parameters are not parsed if they have white space. Therefore, if path2 includes white space (for example, " \file.txt "), the Combine method appends path2 to path1 instead of returning only path2.

.NET Framework and .NET Core versions older than 2.1: Not all invalid characters for directory and file names are interpreted as unacceptable by the Combine method, because you can use these characters for search wildcard characters. For example, while Path.Combine("c:\\", "*.txt") might be invalid if you were to create a file from it, it is valid as a search string. It is therefore successfully interpreted by the Combine method.

For a list of common I/O tasks, see Common I/O Tasks.

See also

Applies to

Combine(String, String, String)

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

Combines three strings into a path.

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

Parameters

path1
String

The first path to combine.

path2
String

The second path to combine.

path3
String

The third path to combine.

Returns

The combined paths.

Exceptions

.NET Framework and .NET Core versions older than 2.1: path1, path2, or path3 contains one or more of the invalid characters defined in GetInvalidPathChars().

path1, path2, or path3 is null.

Examples

The following example combines three paths.

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)

Remarks

path1 should be an absolute path (for example, "d:\archives" or "\\archives\public"). If path2 or path3 is also an absolute path, the combine operation discards all previously combined paths and resets to that absolute path.

Zero-length strings are omitted from the combined path.

If path1 or path2 is not a drive reference (that is, "C:" or "D:") and does not end with a valid separator character as defined in DirectorySeparatorChar, AltDirectorySeparatorChar, or VolumeSeparatorChar, DirectorySeparatorChar is appended to path1 or path2 before concatenation. Note that if path1 or path2 ends in a path separator character that is not appropriate for the target platform, the Combine method preserves the original path separator character and appends a supported one. The following example compares the result on Windows and Unix-based systems when the backslash is used as a path separator character.

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

If path2 does not include a root (for example, if path2 does not start with a separator character or a drive specification), the result is a concatenation of the two paths, with an intervening separator character. If path2 includes a root, path2 is returned.

The parameters are not parsed if they have white space. Therefore, if path2 includes white space (for example, " \file.txt "), the Combine method appends path2 to path1.

.NET Framework and .NET Core versions older than 2.1: Not all invalid characters for directory and file names are interpreted as unacceptable by the Combine method, because you can use these characters for search wildcard characters. For example, while Path.Combine("c:\\", "*.txt") might be invalid if you were to create a file from it, it is valid as a search string. It is therefore successfully interpreted by the Combine method.

See also

Applies to

Combine(String, String, String, String)

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

Combines four strings into a path.

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

Parameters

path1
String

The first path to combine.

path2
String

The second path to combine.

path3
String

The third path to combine.

path4
String

The fourth path to combine.

Returns

The combined paths.

Exceptions

.NET Framework and .NET Core versions older than 2.1: path1, path2, path3, or path4 contains one or more of the invalid characters defined in GetInvalidPathChars().

path1, path2, path3, or path4 is null.

Examples

The following example combines four paths.

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)

Remarks

path1 should be an absolute path (for example, "d:\archives" or "\\archives\public"). If one of the subsequent paths is also an absolute path, the combine operation discards all previously combined paths and resets to that absolute path.

Zero-length strings are omitted from the combined path.

If path1, path2, or path3 is not a drive reference (that is, "C:" or "D:") and does not end with a valid separator character as defined in DirectorySeparatorChar, AltDirectorySeparatorChar, or VolumeSeparatorChar, DirectorySeparatorChar is appended to it before concatenation. Note that if path1, path2, or path3 ends in a path separator character that is not appropriate for the target platform, the Combine method preserves the original path separator character and appends a supported one. The following example compares the result on Windows and Unix-based systems when the backslash is used as a path separator character.

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\

If path2 does not include a root (for example, if path2 does not start with a separator character or a drive specification), the result is a concatenation of the two paths, with an intervening separator character. If path2 includes a root, path2 is returned.

The parameters are not parsed if they have white space. Therefore, if path2 includes white space (for example, " \file.txt "), the Combine method appends path2 to path1.

.NET Framework and .NET Core versions older than 2.1: Not all invalid characters for directory and file names are interpreted as unacceptable by the Combine method, because you can use these characters for search wildcard characters. For example, while Path.Combine("c:\\", "*.txt") might be invalid if you were to create a file from it, it is valid as a search string. It is therefore successfully interpreted by the Combine method.

See also

Applies to