【问题标题】:Getting path of selected folder in VB6在VB6中获取所选文件夹的路径
【发布时间】:2011-06-20 12:42:54
【问题描述】:

我要获取选中的文件夹路径

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

【问题讨论】:

    标签: vb6 common-dialog


    【解决方案1】:

    最短路径:

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

    【讨论】:

      【解决方案2】:

      试试这个

      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)
      

      【讨论】:

      • As Integer 更改为 As String ;)。
      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        这是一个简单的 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)
        

        【讨论】:

          猜你喜欢
          • 2016-07-11
          • 2022-01-23
          • 2015-04-22
          • 1970-01-01
          • 1970-01-01
          • 2023-03-31
          • 2013-07-08
          • 1970-01-01
          • 2016-08-04
          相关资源
          最近更新 更多