VB6中获取选定文件夹的路径

发布于 2024-11-16 18:09:51 字数 489 浏览 8 评论 0原文

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

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" 或任何其他文件夹(仅限用户选择的文件夹)。

I want to get the selected folder path

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

This shows the output "C:\MRMS\Report\xyz.txt", but I want only the selected folder path, ie if the user selects only root(MRMS) folder i.e. "C:\MRMS" or any other folder only up to user selected folder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

相思故 2024-11-23 18:09:51

最短路线:

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, "\")

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, "\")
暖阳 2024-11-23 18:09:51

试试这个

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)

Try this

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

Then, in your code, reference the function like this...

txtLogFile.Text = GetRootDir(dlgBrowse.FileName)
金橙橙 2024-11-23 18:09:51

通过通用对话查找所选文件的文件夹的另一种方法是:

FilePath = Replace(CommonDialog1.FileName, "\" & CommonDialog1.FileTitle, "")

An alternative way to find the folder of selected file by Common Dialogue is this:

FilePath = Replace(CommonDialog1.FileName, "\" & CommonDialog1.FileTitle, "")
嘿看小鸭子会跑 2024-11-23 18:09:51

这是 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)

Here's a simple bare-bones solution for VB5 which doesn't have the Split function:

    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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文