如何将文件名与其完整路径分开 VB .Net

How can I separate the File Name from its full path VB .Net

我需要在 VB Net 中将文件名与其完整路径分开。我正在使用打开的 FileDialog 来获取文件。这就是我的做法

Dim openFileDialog1 As New OpenFileDialog With {
        .InitialDirectory = "c:\",
        .Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*",
        .FilterIndex = 2,
        .RestoreDirectory = True
    }
        If (openFileDialog1.ShowDialog() = DialogResult.OK) Then

        filePath = openFileDialog1.FileName
    End If

通过这段代码,我得到了完整路径和文件名。例如 “C:\mydir\docuemnts\myfile.ext” 现在我想要的是,我想将完整路径与文件名分开,即像这样的“C:\mydir\docuemnts”。

我想以函数的形式做这样的事情,其中我提供完整路径和文件名,return 我是没有文件的路径名字

    Private Shared Function GetSubPathFromString(ByVal fullPath As String) As String
    Dim subpath As String = String.Empty
    ' Logic to Implement this 
    Return subpath
End Function

以上是System.IO.Path的几个基本功能。

'Gets filename with extension
System.IO.Path.GetFileName(filepathandname) 
    
'Gets path up to the filename
System.IO.Path.GetDirectoryName(filepathandname) 

'Gets filename without extention
System.IO.Path.GetFileNameWithoutExtension(filepathandname) 

完整文档在这里:https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getdirectoryname?view=net-6.0

dim directory as string = new FileInfo(yourpath).DirectoryName

如评论之一所述,这对我有用感谢@andrew