本文整理匯總了VB.NET中System.IO.Path.GetDirectoryName方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Path.GetDirectoryName方法的具體用法?VB.NET Path.GetDirectoryName怎麽用?VB.NET Path.GetDirectoryName使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.IO.Path
的用法示例。
在下文中一共展示了Path.GetDirectoryName方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1:
Dim filepath As String = "C:\MyDir\MySubDir\myfile.ext"
Dim directoryName As String
Dim i As Integer = 0
While filepath <> Nothing
directoryName = Path.GetDirectoryName(filepath)
Console.WriteLine("GetDirectoryName('{0}') returns '{1}'", _
filepath, directoryName)
filepath = directoryName
If i = 1
filepath = directoryName + "\" ' this will preserve the previous path
End If
i = i + 1
End While
輸出:
GetDirectoryName('C:\MyDir\MySubDir\myfile.ext') returns 'C:\MyDir\MySubDir' GetDirectoryName('C:\MyDir\MySubDir') returns 'C:\MyDir' GetDirectoryName('C:\MyDir\') returns 'C:\MyDir' GetDirectoryName('C:\MyDir') returns 'C:\' GetDirectoryName('C:\') returns ''
示例2: Module1
' 導入命名空間
Imports System.IO
Module Module1
Sub Main()
Dim P As String = "\"
Try
Console.WriteLine("Starting path: {0}", P)
Console.WriteLine("Directory name: {0}", Path.GetDirectoryName(P))
Console.WriteLine("Extension: {0}", Path.GetExtension(P))
Console.WriteLine("Filename: {0}", Path.GetFileName(P))
Console.WriteLine("Filename without extension: {0}", Path.GetFileNameWithoutExtension(P))
Console.WriteLine("Full path: {0}", Path.GetFullPath(P))
Console.WriteLine("Root: {0}", Path.GetPathRoot(P))
Catch E As Exception
Console.WriteLine("Error: {0}", E.Message)
End Try
Console.WriteLine()
End Sub
End Module