Mono Class Library: System.IO.Path Overview | Members

System.IO.Path.Combine Method

Concatenates two path strings.

public static string Combine (string path1, string path2)

Parameters

path1
A string containing the first path.
path2
A string containing the second path.

Returns

A string containing path1 followed by path2. 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

TypeReason
ArgumentNullExceptionpath1 or path2 is null.
ArgumentExceptionpath1 or path2 contains one or more implementation-specific invalid characters.

Remarks

If path1 does not end with a valid separator character (Path.DirectorySeparatorChar or Path.AltDirectorySeparatorChar), DirectorySeparatorChar is appended to path1 prior to the concatenation.

Example

The following example demonstrates using the Combine method on a Windows system.

C# Example

using System;
using System.IO;
class CombineTest {
 public static void Main() {
 string path1, path2;
 Console.WriteLine("Dir char is {0} Alt dir char is {1}",
 Path.DirectorySeparatorChar,
 Path.AltDirectorySeparatorChar
 );
 path1 = "foo.txt";
 path2 = "\\ecmatest\\examples";
 Console.WriteLine("{0} combined with {1} = {2}",path1, path2 , Path.Combine(path1, 

path2));
 path1 = "\\ecmatest\\examples";
 path2 = "foo.txt";
 Console.WriteLine("{0} combined with {1} = {2}",path1, path2 , Path.Combine(path1, 

path2));
 }
}
   

The output is

Dir char is \ Alt dir char is /
foo.txt combined with \ecmatest\examples = \ecmatest\examples
\ecmatest\examples combined with foo.txt = \ecmatest\examples\foo.txt

Requirements

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0