0

I am trying to set the initial directory to the downloads folder but it doesn't work, Even though it's a perfectly valid path. Here is the code that I am using:

Private Sub btn_AddMod_Click(sender As Object, e As EventArgs) Handles btn_AddMod.Click 'This brings up the file dailoge
    Dim Downloads As String = "\Downloads" 'A variables called \Downloads
    Dim UserprofilePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) 'This finds the directory to the User profile environment variable
    Dim Downloadspath As String = UserprofilePath.Insert(0, "") + Downloads 'This adds \downloads to userpath
    OpenFileDialog1.InitialDirectory = Downloadspath 'This sets the Open File Dialog to go to the users downloads 
    txt_setmodname.Text = Downloadspath 'This is used for debugging, it sets a textbox to the path
    OpenFileDialog1.ShowDialog() 'This opens the Dialog
End Sub

When I copy the output text, the path is perfectly valid but instead of taking me to the path, it takes me to MyDocuments

Comintern
  • 21,855
  • 5
  • 33
  • 80
Ben
  • 49
  • 2
  • 7

2 Answers2

2

That's some wacky code you have there. I'm not sure why it doesn't work and I'm not too interested in finding out. I just tested this and it worked as you want:

Using ofd As New OpenFileDialog
    ofd.InitialDirectory = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads")
    ofd.ShowDialog()
End Using

Obviously you can use an OpenFielDialog that you created in the designer instead of created in code if you want.

By the way, it should be noted that the user's Downloads folder is not necessarily in that location. Mine is on my D: drive, while my personal folder is on my C: drive. For people who keep C: only for system files, all their libraries and the like may be on a secondary drive. Unfortunately, there's no easy way to get the path for the Downloads folder like there is for Documents and some others. I'm guessing that the path is stored in the Registry or the like, but I'm not sure where.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
0

I looked further into it and found out that there is a registry entry for the downloads path, so I used that instead and that seemed to have worked, My code is as follows.

Private Sub btn_AddMod_Click(sender As Object, e As EventArgs) Handles btn_AddMod.Click
    Using ofd As New OpenFileDialog
        Dim DownloadsPath = My.Computer.Registry.GetValue(
            "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\", "{374DE290-123F-4565-9164-39C4925E467B}", Nothing)
        ofd.InitialDirectory = DownloadsPath
        ofd.ShowDialog()
    End Using

I'm not sure why the other method didn't work, it always took me to the MyDocuments folder for some reason.

Ben
  • 49
  • 2
  • 7