2016-12-24 21 views
-1

我试图将初始目录设置为下载文件夹,但它不起作用,即使它是完全有效的路径。下面是我使用的代码:无法将OpenFileDialog初始目录设置为Visual Basic中的下载

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 

当我复制输出文本,路径是完全有效的,但不是带我去的路径,它带我到我的文档

回答

0

我仔细看了看,发现有一个注册表项用于下载路径,所以我用它来代替,而且这似乎奏效了,我的代码如下。

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 

我不知道为什么其他方法不起作用,它总是带我到MyDocuments文件夹出于某种原因。

1

这是一些古怪代码你在那里。我不确定它为什么不起作用,我对找出答案不感兴趣。我只是测试这和它的工作,只要你想:

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

很明显,你可以使用,如果你想你的设计师创造,而不是在代码中创建一个OpenFielDialog

顺便说一下,应该指出,用户的下载文件夹不一定在该位置。我在我的D:驱动器上,而我的个人文件夹在我的C:驱动器上。对于保持C:仅适用于系统文件的用户,其所有库和类似文件可能位于辅助驱动器上。不幸的是,没有简单的方法来获得下载文件夹的路径,就像文档和其他文件一样。我猜测路径存储在注册表或类似的地方,但我不知道在哪里。

相关问题