Path: Combine |
Combines two or more strings into a path.
Public Function Combine( ByRef Path1 As String, ByRef Path2 As String, ParamArray Paths ( ) As Variant ) As String
Path1 should be an absolute path (for example, "d:\archives" or "\\archives\public"). If Path2 or a string in Paths is also an absolute path, the combine operation discards all previously combined paths and resets to that absolute path.
Zero-length strings are omitted from the combined path.
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.
The parameters are not parsed if they have white space. Therefore, if Path2 includes white space (for example, " c:\ "), the Combine method appends Path2 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.
Exception | Condition |
---|---|
ArgumentException | Path1, Path2, or a string element in Paths contains one or more of the invalid characters defined in GetInvalidPathChars. |
The following code example demonstrates using the Combine method.
Public Sub Main() Const Path1 As String = "c:\Temp" Const Path2 As String = "SubDir\File.txt" Const Path3 As String = "c:\Temp.txt" Const Path4 As String = "c:^*&)(_=#'\^.*(.txt" Const Path5 As String = "" Const Path6 As String = vbNullString CombinePaths Path1, Path2 CombinePaths Path1, Path3 CombinePaths Path3, Path2 CombinePaths Path4, Path2 CombinePaths Path5, Path2 CombinePaths Path6, Path2 End Sub Private Sub CombinePaths(ByVal P1 As String, ByVal P2 As String) Dim Combination As String Combination = Path.Combine(P1, P2) Debug.Print CorString.Format("When you combine '{0}' and '{1}', the result is: {2}'{3}'", P1, P2, vbCrLf, Combination) Debug.Print Exit Sub End Sub ' This example code produces the following output. ' ' 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' ' ' When you combine '' and 'SubDir\File.txt', the result is: ' 'SubDir\File.txt'