Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Problem Setting OpenFileDialog.InitialDirectory

Problem Setting OpenFileDialog.InitialDirectory

Scheduled Pinned Locked Moved Visual Basic
csharpvisual-studiowinformshelp
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alan Burkhart
    wrote on last edited by
    #1

    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 the OpenFileDialog 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 If

    Don'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 with cntxMenu.Close(). Otherwise it remains visible on top of the OpenFileDialog. 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.

    L 1 Reply Last reply
    0
    • A Alan Burkhart

      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 the OpenFileDialog 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 If

      Don'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 with cntxMenu.Close(). Otherwise it remains visible on top of the OpenFileDialog. 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.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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.

      A 1 Reply Last reply
      0
      • L Lost User

        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.

        A Offline
        A Offline
        Alan Burkhart
        wrote on last edited by
        #3

        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.

        R M 2 Replies Last reply
        0
        • A Alan Burkhart

          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.

          R Offline
          R Offline
          Ralf Meier
          wrote on last edited by
          #4

          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.

          A 1 Reply Last reply
          0
          • R Ralf Meier

            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.

            A Offline
            A Offline
            Alan Burkhart
            wrote on last edited by
            #5

            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 if AutoUpgradeEnabled 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.

            1 Reply Last reply
            0
            • A Alan Burkhart

              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.

              M Offline
              M Offline
              Member 12886468
              wrote on last edited by
              #6

              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

              A 1 Reply Last reply
              0
              • M Member 12886468

                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

                A Offline
                A Offline
                Alan Burkhart
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups