當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET Path.Combine方法代碼示例

本文整理匯總了VB.NET中System.IO.Path.Combine方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Path.Combine方法的具體用法?VB.NET Path.Combine怎麽用?VB.NET Path.Combine使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.IO.Path的用法示例。


在下文中一共展示了Path.Combine方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: String

Dim paths As String() = {"d:\archives", "2001", "media", "images"}
Dim fullPath As String = Path.Combine(paths)
Console.WriteLine(fullPath)
開發者ID:VB.NET開發者,項目名稱:System.IO,代碼行數:3,代碼來源:Path.Combine

示例2: String

Dim paths As String() = { "d:\archives", "2001", "media", "images" }
Dim fullPath As String = Path.Combine(paths)
Console.WriteLine(fullPath)            

paths = { "d:\archives\", "2001\", "media", "images" }
fullPath = Path.Combine(paths)
Console.WriteLine(fullPath) 

paths = { "d:/archives/", "2001/", "media", "images" }
fullPath = Path.Combine(paths)
Console.WriteLine(fullPath)
開發者ID:VB.NET開發者,項目名稱:System.IO,代碼行數:11,代碼來源:Path.Combine

輸出:

d:\archives\2001\media\images
d:\archives\2001\media\images
d:/archives/2001/media\images

The example displays the following output if run on a Linux system:
d:\archives/2001/media/images
d:\archives\/2001\/media/images
d:/archives/2001/media/images

示例3: ChangeExtensionTest

' 導入命名空間
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

    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
            If p1 = Nothing Then
                p1 = "Nothing"
            End If
            If p2 = Nothing Then
                p2 = "Nothing"
            End If
            Console.WriteLine("You cannot combine '{0}' and '{1}' because: {2}{3}", p1, p2, Environment.NewLine, e.Message)
        End Try
        
        Console.WriteLine()
    End Sub
End Class
開發者ID:VB.NET開發者,項目名稱:System.IO,代碼行數:41,代碼來源:Path.Combine

輸出:

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 '' and 'subdir\file.txt' because: 
Value cannot be null.
Parameter name: path1

示例4:

Dim p1 As String = "d:\archives\"
Dim p2 As String = "media"
Dim p3 As String = "images"
Dim combined As String = Path.Combine(p1, p2, p3)
Console.WriteLine(combined)
開發者ID:VB.NET開發者,項目名稱:System.IO,代碼行數:5,代碼來源:Path.Combine

示例5:

Dim path1 As String = "d:\archives\"
Dim path2 As String = "2001"
Dim path3 As String = "media"
Dim path4 As String = "imaged"
Dim combinedPath As String = Path.Combine(path1, path2, path3, path4)
Console.WriteLine(combined)
開發者ID:VB.NET開發者,項目名稱:System.IO,代碼行數:6,代碼來源:Path.Combine


注:本文中的System.IO.Path.Combine方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。