Concatenates two path strings.
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.
Type Reason ArgumentNullException path1 or path2 is null. ArgumentException path1 or path2 contains one or more implementation-specific invalid characters.
If path1 does not end with a valid separator character (Path.DirectorySeparatorChar or Path.AltDirectorySeparatorChar), DirectorySeparatorChar is appended to path1 prior to the concatenation.
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
Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0