how to get file path using browse button in vb6

1.7k Views Asked by At

I am using this code, it gives the file name like "C:\File\sample.txt". But I am in need of getting path like "C:\File\". How can I get this path?

Private Sub cmdBrowse_Click()
    CommonDialog1.ShowOpen
    txtPath1.Text = CommonDialog1.FileName 
End Sub
2

There are 2 best solutions below

2
Michał Turczyn On

Try

txtPath1.Text = Mid(CommonDialog1.FileName, 1, InStrRev(CommonDialog1.FileName, "\"))
1
Prakash On

In FileName is Your dialog Box FileName that Contains Your File Name..You Need to Remove It By Finding Filename with a \

   With CommonDialog1
    TextBox1.Text = .FileName.Substring(0, .FileName.LastIndexOf("\"))
   End With

This code Finds Last \ from your File Path ("C:\File\sample.txt").Basically File Path Has Filename at the end of File Path.So Find the Last \ and Remove it. this Is working Fine form me.