1

我想获取选定的文件夹路径

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"或任何其他文件夹,直到用户选择的文件夹。

4

4 回答 4

2

The shortest way:

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, "\")
于 2012-10-11T20:22:01.193 回答
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)
于 2011-07-14T14:25:25.797 回答
0

通过 Common Dialogue 查找所选文件的文件夹的另一种方法是:

FilePath = Replace(CommonDialog1.FileName, "\" & CommonDialog1.FileTitle, "")
于 2015-05-31T04:07:47.167 回答
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)
于 2018-06-29T14:47:37.323 回答