Export (0) Print
Expand All

Path.Combine Method

Combines two path strings.

[Visual Basic]
Public Shared Function Combine( _
   ByVal path1 As String, _
   ByVal path2 As String _
) As String
[C#]
public static string Combine(
 string path1,
 string path2
);
[C++]
public: static String* Combine(
 String* path1,
 String* path2
);
[JScript]
public static function Combine(
   path1 : String,
 path2 : String
) : String;

Parameters

path1
The first path.
path2
The second path.

Return Value

A 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.

Exceptions

Exception Type Condition
ArgumentException path1 or path2 contain one or more of the invalid characters defined in InvalidPathChars.
ArgumentNullException path1 or path2 is a null reference (Nothing in Visual Basic).

Remarks

If path1 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 an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Retrieve a file extension. GetExtension
Retrieve the fully qualified path of a file. GetFullPath
Retrieve the file name and extension from a path. GetFileName
Retrieve only the file name from a path. GetFileNameWithoutExtension
Retrieve only the directory name from a path. GetDirectoryName
Change the extension of a file. ChangeExtension
Sort files in a directory by size. GetFileSystemInfos
Determine if a directory exists. Exists
Determine if a file exists. Exists

Example

[Visual Basic, C#, C++] The following example demonstrates using the Combine method on a Windows-based desktop platform.

[Visual Basic] 
Imports System
Imports System.IO

Public Class ChangeExtensionTest
    
    
    Public Shared Sub Main()
        Dim path1 As String = "c:\temp"
        Dim path2 As String = "subdir\file.txt"
        Dim path3 As String = "c:\temp.txt"
        Dim path4 As String = "c:^*&)(_=@#'\\^&#2.*(.txt"
        Dim path5 As String = ""
        Dim path6 As String = Nothing

        CombinePaths(path1, path2)
        CombinePaths(path1, path3)
        CombinePaths(path3, path2)
        CombinePaths(path4, path2)
        CombinePaths(path5, path2)
        CombinePaths(path6, path2)
    End Sub 'Main

    Private Shared Sub CombinePaths(p1 As String, p2 As String)
        
        Try
            Dim combination As String = Path.Combine(p1, p2)
            
            Console.WriteLine("When you combine '{0}' and '{1}', the result is: {2}'{3}'", p1, p2, Environment.NewLine, combination)
        Catch e As Exception
            Console.WriteLine("You cannot combine '{0}' and '{1}' because: {2}{3}", p1, p2, Environment.NewLine, e.Message)
        End Try
        
        Console.WriteLine()
    End Sub 'CombinePaths
End Class 'ChangeExtensionTest

[C#] 
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 = "";
        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) {
            Console.WriteLine("You cannot combine '{0}' and '{1}' because: {2}{3}",
                        p1, p2, Environment.NewLine, e.Message);
        }

        Console.WriteLine();
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

void CombinePaths(String* p1, String* p2) {
    try {
        String* combination = Path::Combine(p1, p2);
        Console::WriteLine(S"When you combine '{0}' and '{1}', the result is: {2}'{3}'",
            p1, p2, Environment::NewLine, combination);
    } catch (Exception* e) {
        Console::WriteLine(S"You cannot combine '{0}' and '{1}' because: {2}{3}",
            p1, p2, Environment::NewLine, e->Message);
    }
    Console::WriteLine();
}

int main() {

    String* path1 = S"c:\\temp";
    String* path2 = S"subdir\\file.txt";
    String* path3 = S"c:\\temp.txt";
    String* path4 = S"c:^*&)(_=@#'\\^&#2.*(.txt";
    String* path5 = S"";
    String* path6 = 0;

    CombinePaths(path1, path2);
    CombinePaths(path1, path3);
    CombinePaths(path3, path2);
    CombinePaths(path4, path2);
    CombinePaths(path5, path2);
    CombinePaths(path6, path2);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Path Class | Path Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2014 Microsoft