-1

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
GSerg
  • 76,472
  • 17
  • 159
  • 346

2 Answers2

0

Try

txtPath1.Text = Mid(CommonDialog1.FileName, 1, InStrRev(CommonDialog1.FileName, "\"))
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
0

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.

Prakash
  • 100
  • 10
  • This answer concerns VB.NET. If the OP was using VB.NET (which they do not appear to), then this answer [would be wrong](https://stackoverflow.com/q/5229292/11683). – GSerg Apr 23 '18 at 08:30