Problem Setting OpenFileDialog.InitialDirectory
-
Using Win 7 / Winforms / VS 2013 / VB.net My application stores the last several visited directories in the File menu. But when I click an item in the drop-down to set the
.InitialDirectory
property theOpenFileDialog
just goes back to the last directory viewed. I don't need to store a specific file name, I just need the dialog to open in the desired directory. I've checked the text of the menu items and it's all correct. I also tried hard-coding a directory name and it still goes back to the first directory opened during the session. Code is below:ofd.InitialDirectory = e.ClickedItem.Text
cntxMenu.Close()
res = ofd.ShowDialog
If res = Windows.Forms.DialogResult.OK Then
PopulateListview(ofd.FileName)
End IfDon't know if this is related: This is a
ContextMenuStrip
that opens when a button is clicked. There are two items with dropdowns. One is a zoom menu that works fine. But with the recent directories menu, I have to close it withcntxMenu.Close()
. Otherwise it remains visible on top of theOpenFileDialog
. What am I doing wrong here?Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.
-
Using Win 7 / Winforms / VS 2013 / VB.net My application stores the last several visited directories in the File menu. But when I click an item in the drop-down to set the
.InitialDirectory
property theOpenFileDialog
just goes back to the last directory viewed. I don't need to store a specific file name, I just need the dialog to open in the desired directory. I've checked the text of the menu items and it's all correct. I also tried hard-coding a directory name and it still goes back to the first directory opened during the session. Code is below:ofd.InitialDirectory = e.ClickedItem.Text
cntxMenu.Close()
res = ofd.ShowDialog
If res = Windows.Forms.DialogResult.OK Then
PopulateListview(ofd.FileName)
End IfDon't know if this is related: This is a
ContextMenuStrip
that opens when a button is clicked. There are two items with dropdowns. One is a zoom menu that works fine. But with the recent directories menu, I have to close it withcntxMenu.Close()
. Otherwise it remains visible on top of theOpenFileDialog
. What am I doing wrong here?Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.
-
I have just tried a test (Windows 10) and it works correctly. I can only suggest you try using your debugger to see what actually happens as the code runs.
I was pretty much reduced to throwing code at the wall to see what would stick. Setting the dialog's
AutoUpgradeEnabled
property to True caused it to behave normally. I'll freely admit to having no idea why. But as it's an application strictly for my own use I'm not going to worry over it at this point. :)Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.
-
I was pretty much reduced to throwing code at the wall to see what would stick. Setting the dialog's
AutoUpgradeEnabled
property to True caused it to behave normally. I'll freely admit to having no idea why. But as it's an application strictly for my own use I'm not going to worry over it at this point. :)Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.
I can remember that the path for the InitialDirectory must be written in a special way. Assuming that the path you give to the Property of the Dialog is existing and correct written but you should look, if the string ends with a "\" or not. Perhaps you should change this.
-
I can remember that the path for the InitialDirectory must be written in a special way. Assuming that the path you give to the Property of the Dialog is existing and correct written but you should look, if the string ends with a "\" or not. Perhaps you should change this.
Added a backslash to the strings but that didn't do it either. Perhaps there is a bug that causes the
OpenFileDialog
to not recognize the string ifAutoUpgradeEnabled
is false. Seems highly unlikely though. I may create a temporary project and see if I can duplicate the problem there. If not then I'll at least know the problem is unique to one project. I have another app that maintains a list of recent files (not directories) and it's been working fine for years.Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.
-
I was pretty much reduced to throwing code at the wall to see what would stick. Setting the dialog's
AutoUpgradeEnabled
property to True caused it to behave normally. I'll freely admit to having no idea why. But as it's an application strictly for my own use I'm not going to worry over it at this point. :)Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.
I struggled with the same problem. When I set the AutoUpgradeEnabled to True, it started working better. But I added the below code and it seems to always works now.
Public Class frmSample
Dim openFileDialog1 As New OpenFileDialog() Dim sTemp As String Private Sub btnOpen\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click openFileDialog1.AutoUpgradeEnabled = True sTemp = "None" sTemp = OpenDir() Do Until sTemp <> "None" 'Nothing Loop 'The above loop is my solution. 'It makes sure that the initial directory is known, before 'continuing to If OpenFileDialog1.ShowDialog openFileDialog1.Filter = "txt files (\*.txt)|\*.txt|All files (\*.\*)|\*.\*" openFileDialog1.FilterIndex = 0 openFileDialog1.RestoreDirectory = True If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then 'Enter your code End If End Sub Private Function OpenDir() As String openFileDialog1.InitialDirectory = "C:\\Sample Initial Directory" Return "OpenDir" End Function
End Class
-
I struggled with the same problem. When I set the AutoUpgradeEnabled to True, it started working better. But I added the below code and it seems to always works now.
Public Class frmSample
Dim openFileDialog1 As New OpenFileDialog() Dim sTemp As String Private Sub btnOpen\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click openFileDialog1.AutoUpgradeEnabled = True sTemp = "None" sTemp = OpenDir() Do Until sTemp <> "None" 'Nothing Loop 'The above loop is my solution. 'It makes sure that the initial directory is known, before 'continuing to If OpenFileDialog1.ShowDialog openFileDialog1.Filter = "txt files (\*.txt)|\*.txt|All files (\*.\*)|\*.\*" openFileDialog1.FilterIndex = 0 openFileDialog1.RestoreDirectory = True If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then 'Enter your code End If End Sub Private Function OpenDir() As String openFileDialog1.InitialDirectory = "C:\\Sample Initial Directory" Return "OpenDir" End Function
End Class
Thanks. I ended up doing something similar but that was a couple of years back. I don't remember exactly how I did it. I should have posted the solve here but apparently I didn't think about it. Thanks again. :)
Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.