[英]VB.NET Get directory and sub-directory from path
I'm trying to get the full path minus the filename from a path in vb.net.我试图从 vb.net 中的路径中获取完整路径减去文件名。 I'm using a textbox and a browse button to get the full path, but I want to save just the directories.
我正在使用文本框和浏览按钮来获取完整路径,但我只想保存目录。
How do I get just the directories and sub-directories?如何仅获取目录和子目录?
Example file name:示例文件名:
Desired result:期望的结果:
c:\Users\jsmith\Desktop\ c:\用户\jsmith\桌面\
Protected Sub btnBackupFolderName_Click(sender As Object, e As EventArgs) Handles btnBackupFolderName.Click ' Call ShowDialog. Dim result As DialogResult = openFD.ShowDialog() ' Test result. If result = Windows.Forms.DialogResult.OK Then Dim FileNameText As String = openFD.FileName.ToString() Dim backupFolderName = Path.GetFileName(Path.GetDirectoryName(FileNameText)) 'this just gives me 'Desktop' txtBackupFolder.Text = di.ToString 'backupFolderName End If End Sub
Thank you for your help!谢谢您的帮助!
Get the full path of the file and then use GetDirectoryName() to retrieve the folder-path, like this:获取文件的完整路径,然后使用 GetDirectoryName() 检索文件夹路径,如下所示:
Dim openFD As OpenFileDialog = New OpenFileDialog()
Dim result As DialogResult = openFD.ShowDialog()
If result = DialogResult.OK Then
Dim FileNameText As String = openFD.FileName.ToString()
' This gets the folder-path, sans filename.
txtBackupFolder.Text = Path.GetDirectoryName(openFD.FileName)
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.