Path.Combine Method
Combines two path strings.
Assembly: mscorlib (in mscorlib.dll)
Return Value
Type: System.StringA string containing 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.
| Exception | Condition |
|---|---|
| ArgumentException |
path1 or path2 contain one or more of the invalid characters defined in GetInvalidPathChars. |
| ArgumentNullException |
path1 or path2 is null. |
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.
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.
Because the parameters are not parsed if they have white space, if path2 is " c:\\ ", this will be appended to path1 instead of returning only path2.
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.
The following code example demonstrates using the Combine method on a Windows-based desktop platform.
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:^*&)(_=@#'\\^.*(.txt"; string path5 = ""; string path6 = null; CombinePaths(path1, path2); CombinePaths(path1, path3); CombinePaths(path3, path2); CombinePaths(path4, path2); CombinePaths(path5, path2); CombinePaths(path6, path2); } private static 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 == null) p1 = "null"; if (p2 == null) p2 = "null"; Console.WriteLine("You cannot combine '{0}' and '{1}' because: {2}{3}", p1, p2, Environment.NewLine, e.Message); } 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:^*&)(_=@#'\^.*(.txt' and 'subdir\file.txt', the result is: // 'c:^*&)(_=@#'\^.*(.txt\subdir\file.txt' // // When you combine '' and 'subdir\file.txt', the result is: // 'subdir\file.txt' // // You cannot combine 'null' and 'subdir\file.txt' because: // Value cannot be null. // Parameter name: path1
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Dim part1 as String = "c:\temp\"
Dim part2 as String = "\theseFiles\"
Dim part3 as String = "thoseFiles"
Dim result as String
result = Path.Combine(part1, part2)
'result value is "\theseFiles\"
result = Path.Combine(part1, part3)
'result value is "c:\temp\thoseFiles\"
The first result is not what was expected, since a directory seperator is technically not a root.
Roots should be recognized as either a UNC root "\\" or a drive letter root "N:\". This is a subtle bug that can cause issues with your application.
- 1/7/2009
- Andrew Stanton