我想获取所选的文件夹路径
dlgBrowse.ShowOpen
fname = dlgBrowse.FileName
dlgBrowse.Filter = "Text File (*.txt)|*.txt|Log File (*.log)|*.log||All Files (*.*)|*.*"
dlgBrowse.DialogTitle = "Open Log File"
dlgBrowse.ShowOpen
If dlgBrowse.FileName <> "" Then
txtLogFile.Text = dlgBrowse.FileName
End If
MsgBox fname
这显示了输出"C:\MRMS\Report\xyz.txt"
,但我只想要选择的文件夹路径,即用户只选择根(MRMS)文件夹即"C:\MRMS"
或任何其他文件夹,直到用户选择的文件夹。
答案 0 :(得分:2)
最短路:
Dim FullPath as string, ParentFolder as string, l() as string
FullPath = "" '... Write here the path from ComDlg
l = Split(FullPath, "\")
l(UBound(l)) = ""
ParentFolder = Join(l, "\")
答案 1 :(得分:1)
试试这个
Private Function GetRootDir(ByVal inputString As String) As Integer
'min real path is c:\. We need a len of at least 2
If Len(inputString) < 2 Then
GetRootDir = ""
End If
Dim t As Integer, s As Integer
t = InStr(1, inputString, "\")
If t < 1 Then
GetRootDir = ""
Exit Function
End If
s = InStr(t + 1, inputString, "\")
'If this is the root folder that was selected
If s < 1 Then
GetRootDir = Mid(inputString, t + 1)
Exit Function
End If
GetRootDir = Mid(inputString, t + 1, s - t - 1)
End Function
然后,在您的代码中,引用这样的函数......
txtLogFile.Text = GetRootDir(dlgBrowse.FileName)
答案 2 :(得分:0)
通过Common Dialogue查找所选文件的文件夹的另一种方法是:
FilePath = Replace(CommonDialog1.FileName, "\" & CommonDialog1.FileTitle, "")
答案 3 :(得分:0)
这是针对VB5的简单的准系统解决方案,其中没有Split功能:
For Num = Len(CommonDialog1.filename) To 4 Step -1
Charac = Mid$(CommonDialog1.filename, Num, 1)
If Charac = "\" Then Exit For
Next
LastSlashPos = Num
LastPath = Left$(CommonDialog1.filename, LastSlashPos)