Click here to Skip to main content
15,918,889 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I click a button I am trying to have my program open a folder, "PHRF List", using OpenFileDialog.InitialDirectory = "C:\Race\PHRF List\". The code takes me to "C:\Race\" but not to the next level folder PHRF List. Does InitialDirectory open only the first level below C:? Here is the complete code for this Sub:

Private Sub btnLoadList_Click(sender As Object, e As EventArgs) Handles btnLoadList.Click
       If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
           OpenFileDialog1.InitialDirectory = "C:\Race\PHRF List\"
           OpenFileDialog1.Filter = "txt files(*.txt)|*.txt"                   '
           txtPHRFfile.Text = OpenFileDialog1.FileName
           OpenFileDialog1.RestoreDirectory = True
       End If
       Thread.Sleep(500) 'Enter a delay here
       Using readFile As New StreamReader(txtPHRFfile.Text)
           Do While readFile.Peek() <> -1
               blPHRFstr = readFile.ReadLine()
               listArr = Split(blPHRFstr, ",")
               Me.dgv21.Rows.Add(listArr)
           Loop
           readFile.Close()
           readFile.Dispose()
       End Using
   End Sub


What I have tried:

Read many on line articles including Microsoft tutorial on using InitialDirectory but none try to go more than one level below C:

Also added OpenFileDialog.RestoreDirectory but it has no effect.
Posted
Updated 6-Nov-18 20:36pm

1 solution

PLease, look at your code:
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
   OpenFileDialog1.InitialDirectory = "C:\Race\PHRF List\"
   OpenFileDialog1.Filter = "txt files(*.txt)|*.txt"                   '
   txtPHRFfile.Text = OpenFileDialog1.FileName
   OpenFileDialog1.RestoreDirectory = True
End If
You open the dialog before you set the Initial Directory!
Try this:
OpenFileDialog1.InitialDirectory = "C:\Race\PHRF List\"
OpenFileDialog1.Filter = "txt files(*.txt)|*.txt"                   '
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
   txtPHRFfile.Text = OpenFileDialog1.FileName
   OpenFileDialog1.RestoreDirectory = True
End If
 
Share this answer
 
Comments
Member 10628309 7-Nov-18 13:18pm    
You are very correct and I thank you very much for your solution.
Ed
OriginalGriff 7-Nov-18 13:58pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900