Combine Method

Path.Combine Method

Combines two path strings.

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

Public Shared Function Combine ( _
	path1 As String, _
	path2 As String _
) As String
Dim path1 As String
Dim path2 As String
Dim returnValue As String

returnValue = Path.Combine(path1, path2)
public static String Combine (
	String path1, 
	String path2
public static function Combine (
	path1 : String, 
	path2 : String
) : String



The first path.


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.

Exception typeCondition


path1 or path2 contain one or more of the invalid characters defined in InvalidPathChars, or contains a wildcard character.


path1 or path2 is a null reference (Nothing in Visual Basic).

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

How to: Write Text to a File

Write to a text file.

How to: Write Text to a File

Read from a text file.

How to: Read Text from a File

Retrieve a file extension.


Retrieve the fully qualified path of a file.


Retrieve the file name and extension from a path.


Retrieve only the file name from a path.


Retrieve only the directory name from a path.


Change the extension of a file.


Sort files in a directory by size.


Determine if a directory exists.


Determine if a file exists.


The following code example demonstrates using the Combine method on a Windows-based desktop platform.

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)
            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
    End Sub 'CombinePaths
End Class 'ChangeExtensionTest
' 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:^*&)(_=@#'\^&#2.*(.txt' and 'subdir\file.txt', the result is: 
' 'c:^*&)(_=@#'\^&#2.*(.txt\subdir\file.txt'
' When you combine '' and 'subdir\file.txt', the result is: 
' 'subdir\file.txt'
' You cannot combine '' and 'subdir\file.txt' because: 
' Value cannot be null.
' Parameter name: path1

import System.*;  
import System.IO.*;  

public class ChangeExtensionTest
    public static void main(String[] args)
        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);
    } //main

    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}'", new Object[] { p1, p2, 
                Environment.get_NewLine(), combination });
        catch (System.Exception e) {
            Console.WriteLine("You cannot combine '{0}' and '{1}' because: "
                + "{2}{3}", new Object[] { p1, p2, Environment.get_NewLine(), 
                e.get_Message() });
    } //CombinePaths
} //ChangeExtensionTest

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

© 2016 Microsoft