ȫ·ȡһ·ȫ·VB.net
'֧VB6VBA

'Imports System.IO
 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        
        'ȫ·ȡһ·ȫ·
        Dim Str As String
        Str = "C:\Users\zyg365\Desktop\zyg365"  'ļʱ
        MsgBox(Path.GetDirectoryName(Str)) 'أC:\Users\zyg365\Desktop

        Str = "C:\Users\zyg365\Desktop\zyg365\HR.txt" 'ļʱ
        MsgBox(Path.GetDirectoryName(Str)) 'أC:\Users\zyg365\Desktop\zyg365

    End Sub


2ַⲿãVBVBAVB.netͨ
   ͨ InStrRev  Left ȡ·
 Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        MsgBoxGetParentPath("C:\Folder1\Folder2\")   'أC:\Folder1

        MsgBoxGetParentPath("C:\Folder1\Folder2")   'أC:\Folder1

        MsgBoxGetParentPath("C:\Folder1\Folder2\zyg365.txt")   'أC:\Folder1\Folder2\
 End Sub

    Function GetParentPath(ByVal fullPath As String) As String
        If Strings.Right(fullPath, 1) = "\" Then
            fullPath = Strings.Left(fullPath, Len(fullPath) - 1)  ' Ƴĩβ\
        End If
        Dim lastPos As Long
        lastPos = InStrRev(fullPath, "\")
        If lastPos > 0 Then
            GetParentPath = Strings.Left(fullPath, lastPos - 1)
        Else
            GetParentPath = ""  ' Ŀ¼޸Ŀ¼
        End If
    End Function