Path.Combine 方法

合并两个路径字符串。

**命名空间:**System.IO
**程序集:**mscorlib(在 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 String^ Combine (
    String^ path1, 
    String^ path2
)
public static String Combine (
    String path1, 
    String path2
)
public static function Combine (
    path1 : String, 
    path2 : String
) : String

参数

  • path1
    第一个路径。
  • path2
    第二个路径。

返回值

包含合并的路径的字符串。如果指定的路径之一是零长度字符串,则该方法返回其他路径。如果 path2 包含绝对路径,则该方法返回 path2。

异常

异常类型 条件

ArgumentException

path1 或 path2 包含一个或多个在 InvalidPathChars 中定义的无效字符或包含通配符。

ArgumentNullException

path1 或 path2 为 空引用(在 Visual Basic 中为 Nothing)。

备注

如果 path1 不是一个驱动器引用(即不是“C:”或“D:”)而且不是以 DirectorySeparatorCharAltDirectorySeparatorCharVolumeSeparatorChar 中定义的有效分隔符结束,则在串联前将 DirectorySeparatorChar 追加到 path1 中。

如果 path2 不包括根(例如,如果 path2 没有以分隔符或驱动器规格起始),则结果是两个路径的串联,具有介于其间的分隔符。如果 path2 包括根,则返回 path2。

由于有空白时不进行参数分析,如果 path2 为“ c:\\ ”,则将其追加到 path1,而不是仅返回 path2。

不是目录和文件名的所有无效字符都被 Combine 方法解释为不可接受的,因为您可以将这些字符用于搜索通配符。例如,尽管 Path.Combine("c:\\", "*.txt") 可能是无效的(如果您要根据它创建一个文件),但它作为搜索字符串是有效的。因此 Combine 方法成功解释它。

有关使用此方法的示例,请参见下面的“示例”部分。下表列出了其他典型或相关的 I/O 任务的示例。

若要执行此操作...

请参见本主题中的示例...

创建文本文件。

如何:向文件写入文本

写入文本文件。

如何:向文件写入文本

读取文本文件。

如何:从文件读取文本

检索文件扩展名。

GetExtension

检索文件的完全限定路径。

GetFullPath

检索路径中的文件名和扩展名。

GetFileName

只检索路径中的文件名。

GetFileNameWithoutExtension

只检索路径中的目录名。

GetDirectoryName

更改文件扩展名。

ChangeExtension

按大小对目录中的文件排序。

GetFileSystemInfos

确定目录是否存在。

Exists

确定文件是否存在。

Exists

示例

下面的代码示例演示如何在基于 Windows 的桌面平台上使用 Combine 方法。

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
' 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
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();
    }
}
// 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
using namespace System;
using namespace System::IO;
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();
}

int main()
{
   String^ path1 = "c:\\temp";
   String^ path2 = "subdir\\file.txt";
   String^ path3 = "c:\\temp.txt";
   String^ path4 = "c:^*&)(_=@#'\\^&#2.*(.txt";
   String^ path5 = "";
   String^ path6 = nullptr;
   CombinePaths( path1, path2 );
   CombinePaths( path1, path3 );
   CombinePaths( path3, path2 );
   CombinePaths( path4, path2 );
   CombinePaths( path5, path2 );
   CombinePaths( path6, path2 );
}
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() });
        }
        Console.WriteLine();
    } //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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Path 类
Path 成员
System.IO 命名空间

其他资源

文件和流 I/O
如何:从文件读取文本
如何:向文件写入文本