0 out of 1 rated this helpful - Rate this topic

Path.Combine Method (String[])

Combines an array of strings into a path.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)
public static string Combine(
	params string[] paths
)

Parameters

paths
Type: System.String[]
An array of parts of the path.

Return Value

Type: System.String
The combined paths.
Exception Condition
ArgumentException

One of the strings in the array contains one or more of the invalid characters defined in GetInvalidPathChars.

ArgumentNullException

One of the strings in the array is null.

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.

Zero-length strings are omitted from the combined path.

The parameters are not parsed if they have white space.

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.

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


.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Re: No path separator is added after disk letter
@Ketil A This behaviour makes sense. A drive letter path without the following backslash is a valid path that refers to the current working directory on that drive. E.g. if at a command prompt my current directory in C: is C:\dev, and I type "cd C:project1", the expected behaviour is that the current directory in C: changes to C:\dev\project1, not C:\project1.
No path separator is added after disk letter

if you split a complete path on the directory separator, and then combine them, you will loose the rootmost directory separator:

  string pth = "c:\\root\\a\b.c";
  string ptharray = pth.Split(Path.DirectorySeparatorChar);
  string recombined =Path.Combine(ptharray);

recombined will receive the string

   c:root\a\b.c

without the intial separator 

No path separator is added after disk letter

if you split a complete path on the directory separator, and then combine them, you will loose the rootmost directory separator:

  string pth = "c:\\root\\a\b.c";
  string ptharray = pth.Split(Path.DirectorySeparatorChar);
  string recombined =Path.Combine(ptharray);

recombined will receive the string

   c:root\a\b.c

without the intial separator