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. How to browse folder

How to browse folder

Scheduled Pinned Locked Moved Visual Basic
tutorial
16 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.
  • T Offline
    T Offline
    trushitshah
    wrote on last edited by
    #1

    How to browse folder not file

    T 1 Reply Last reply
    0
    • T trushitshah

      How to browse folder not file

      T Offline
      T Offline
      Tom Deketelaere
      wrote on last edited by
      #2

      Look at the system.io.directory namespace you have everything you need there For GUI searching there is a control 'FolderBrowserDialog' located in the toolbox under the group 'Dialogs'

      T 1 Reply Last reply
      0
      • T Tom Deketelaere

        Look at the system.io.directory namespace you have everything you need there For GUI searching there is a control 'FolderBrowserDialog' located in the toolbox under the group 'Dialogs'

        T Offline
        T Offline
        trushitshah
        wrote on last edited by
        #3

        It's giving error at line FolderBrowserDialog.ShowDialog()

        T 1 Reply Last reply
        0
        • T trushitshah

          It's giving error at line FolderBrowserDialog.ShowDialog()

          T Offline
          T Offline
          Tom Deketelaere
          wrote on last edited by
          #4

          Since my telepathic skills are a bit off today: What error? How did you initialize the dialog window? How did you declare the dialog window? Did you add the control in designer or in code?

          T 1 Reply Last reply
          0
          • T Tom Deketelaere

            Since my telepathic skills are a bit off today: What error? How did you initialize the dialog window? How did you declare the dialog window? Did you add the control in designer or in code?

            T Offline
            T Offline
            trushitshah
            wrote on last edited by
            #5

            I used this code for browse folder Dim theFolderBrowser As New FolderBrowserDialog theFolderBrowser.Description = "Please locate Directory..." theFolderBrowser.ShowNewFolderButton = False theFolderBrowser.RootFolder = Environment.SpecialFolder.Desktop theFolderBrowser.SelectedPath = My.Computer.FileSystem.SpecialDirectories.ProgramFiles If theFolderBrowser.ShowDialog = Windows.Forms.DialogResult.OK Then txtpath.Text = theFolderBrowser.SelectedPath Else MessageBox.Show(theFolderBrowser.SelectedPath) End If It's give me error at If theFolderBrowser.ShowDialog = Windows.Forms.DialogResult.OK Then error:Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.

            T J 2 Replies Last reply
            0
            • T trushitshah

              I used this code for browse folder Dim theFolderBrowser As New FolderBrowserDialog theFolderBrowser.Description = "Please locate Directory..." theFolderBrowser.ShowNewFolderButton = False theFolderBrowser.RootFolder = Environment.SpecialFolder.Desktop theFolderBrowser.SelectedPath = My.Computer.FileSystem.SpecialDirectories.ProgramFiles If theFolderBrowser.ShowDialog = Windows.Forms.DialogResult.OK Then txtpath.Text = theFolderBrowser.SelectedPath Else MessageBox.Show(theFolderBrowser.SelectedPath) End If It's give me error at If theFolderBrowser.ShowDialog = Windows.Forms.DialogResult.OK Then error:Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.

              T Offline
              T Offline
              Tom Deketelaere
              wrote on last edited by
              #6

              Are you using this code in a separate thread than the main (GUI) thread? If so you can not do this (at least not that I'm aware off), all dialogs and GUI aspects of your program have to be executed by the main thread.

              T 1 Reply Last reply
              0
              • T Tom Deketelaere

                Are you using this code in a separate thread than the main (GUI) thread? If so you can not do this (at least not that I'm aware off), all dialogs and GUI aspects of your program have to be executed by the main thread.

                T Offline
                T Offline
                trushitshah
                wrote on last edited by
                #7

                Thanks for yr continuos replay.. I don't know about separate thread than the main (GUI) thread..

                T 1 Reply Last reply
                0
                • T trushitshah

                  Thanks for yr continuos replay.. I don't know about separate thread than the main (GUI) thread..

                  T Offline
                  T Offline
                  Tom Deketelaere
                  wrote on last edited by
                  #8

                  Where are you calling this code? Is it in the load of a form, in the click event of a button, in a separate class, in a background worker class,...? Try adding the STAThreadattribute to the main function (the new constructor of you startupform) like the error states. I'v never encountered this error before so you might have to google it a bit. If you are just calling this dialog after the click of the event try adding a folderbrowserdialog onto the form by dragging it out off the toolbox (like you would do with any other control) and using that one.

                  T 1 Reply Last reply
                  0
                  • T Tom Deketelaere

                    Where are you calling this code? Is it in the load of a form, in the click event of a button, in a separate class, in a background worker class,...? Try adding the STAThreadattribute to the main function (the new constructor of you startupform) like the error states. I'v never encountered this error before so you might have to google it a bit. If you are just calling this dialog after the click of the event try adding a folderbrowserdialog onto the form by dragging it out off the toolbox (like you would do with any other control) and using that one.

                    T Offline
                    T Offline
                    trushitshah
                    wrote on last edited by
                    #9

                    I m calling this code in button click event.. I not found control on toolbox like folderbroswedialog.

                    T 1 Reply Last reply
                    0
                    • T trushitshah

                      I m calling this code in button click event.. I not found control on toolbox like folderbroswedialog.

                      T Offline
                      T Offline
                      Tom Deketelaere
                      wrote on last edited by
                      #10

                      What version of VS are you using because it's a standard control, it should be in your toolbox. Check the 'All Windows Forms' group. Or check if you can add it with the 'choose items' dialog (right click on an empty space in the toolbox). Also check if you have the system.windows.forms reference (you should have that but check it anyway) If you can't find it in any off those I'm afraid there is probably something wrong with your VS installation, and that's most likely why you are getting that error then. (I just copy-pasted your code in a clean project with one form and one button and it works perfectly, so check if there is nothing wrong with the references, with your installation,...)

                      T 1 Reply Last reply
                      0
                      • T Tom Deketelaere

                        What version of VS are you using because it's a standard control, it should be in your toolbox. Check the 'All Windows Forms' group. Or check if you can add it with the 'choose items' dialog (right click on an empty space in the toolbox). Also check if you have the system.windows.forms reference (you should have that but check it anyway) If you can't find it in any off those I'm afraid there is probably something wrong with your VS installation, and that's most likely why you are getting that error then. (I just copy-pasted your code in a clean project with one form and one button and it works perfectly, so check if there is nothing wrong with the references, with your installation,...)

                        T Offline
                        T Offline
                        trushitshah
                        wrote on last edited by
                        #11

                        I m using VS 2005 and .Net framework 2.0. I found folderdialog in choose items.It is already checked. But in toolbox it will not dispalyed. That's why i cn't drag from toolbox.

                        T 1 Reply Last reply
                        0
                        • T trushitshah

                          I m using VS 2005 and .Net framework 2.0. I found folderdialog in choose items.It is already checked. But in toolbox it will not dispalyed. That's why i cn't drag from toolbox.

                          T Offline
                          T Offline
                          Tom Deketelaere
                          wrote on last edited by
                          #12

                          Try unchecking it, closing the choose items dialog, reopen the choose items, rechecking it. (maybe even add it to a custom group in the toolbox so you can easily find it after (create the group then first and right click in the empty area off that group) If this doesn't work there is something wrong with your installation of VS.

                          T 1 Reply Last reply
                          0
                          • T Tom Deketelaere

                            Try unchecking it, closing the choose items dialog, reopen the choose items, rechecking it. (maybe even add it to a custom group in the toolbox so you can easily find it after (create the group then first and right click in the empty area off that group) If this doesn't work there is something wrong with your installation of VS.

                            T Offline
                            T Offline
                            trushitshah
                            wrote on last edited by
                            #13

                            Thanks a lot for yr help.. I will try this . But if u find any other code than this then plz send me.

                            L 1 Reply Last reply
                            0
                            • T trushitshah

                              Thanks a lot for yr help.. I will try this . But if u find any other code than this then plz send me.

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

                              Is this for a web application...?

                              Check out the CodeProject forum Guidelines[^]

                              1 Reply Last reply
                              0
                              • T trushitshah

                                I used this code for browse folder Dim theFolderBrowser As New FolderBrowserDialog theFolderBrowser.Description = "Please locate Directory..." theFolderBrowser.ShowNewFolderButton = False theFolderBrowser.RootFolder = Environment.SpecialFolder.Desktop theFolderBrowser.SelectedPath = My.Computer.FileSystem.SpecialDirectories.ProgramFiles If theFolderBrowser.ShowDialog = Windows.Forms.DialogResult.OK Then txtpath.Text = theFolderBrowser.SelectedPath Else MessageBox.Show(theFolderBrowser.SelectedPath) End If It's give me error at If theFolderBrowser.ShowDialog = Windows.Forms.DialogResult.OK Then error:Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.

                                J Offline
                                J Offline
                                JR212
                                wrote on last edited by
                                #15

                                I might be mistaken but: programfolder is not part of the desktopfolder so it can't change your rootfolder or selectedpath

                                T 1 Reply Last reply
                                0
                                • J JR212

                                  I might be mistaken but: programfolder is not part of the desktopfolder so it can't change your rootfolder or selectedpath

                                  T Offline
                                  T Offline
                                  Tom Deketelaere
                                  wrote on last edited by
                                  #16

                                  That doesn't brake the code. It simply means that the rootfolder property is ignored. And upon opening the dialog the program folder will be the one selected.

                                  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