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

最短の方法:

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

これは、Split 関数を持たない VB5 の単純な必要最小限のソリューションです。

    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 に答える