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 string Combine (scoped ReadOnlySpan<string> paths);

Parameters

paths
ReadOnlySpan<String>

A span of parts of the path.

Returns

The combined paths.

Applies to

.NET 9
Product Versions
.NET 9

Combine(String[])

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

Combines an array of strings into a path.

public static string Combine (params string[] paths);

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);

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

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

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Combine(String, String)

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

Combines two strings into a path.

public static string Combine (string path1, string path2);

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

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

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

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Combine(String, String, String)

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

Combines three strings into a path.

public static string Combine (string path1, string path2, string path3);

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);

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

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

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Combine(String, String, String, String)

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

Combines four strings into a path.

public static string Combine (string path1, string path2, string path3, string path4);

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);

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\

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

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1