Results 1 to 7 of 7

Thread: [RESOLVED] FolderBrowserDialog

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Location
    Norway
    Posts
    24

    Resolved [RESOLVED] FolderBrowserDialog

    Hi.
    Do anyone know if its possible to set the rootfolder of a FolderBrowserDialog box to only show my network places (xp) / Network (Vista). It's not available as a Environment.SpecialFolder in vb.net.

    like the dialog you get when selecting map network drive -> Browse in windows explorer, see screenshot below.
    Attached Images Attached Images  

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: FolderBrowserDialog

    try this:

    vb Code:
    1. Dim fbd As New FolderBrowserDialog
    2. Dim type As Type = fbd.[GetType]
    3. '======== Get Fieldinfo for rootfolder.
    4. Dim fieldInfo As Reflection.FieldInfo = type.GetField("rootFolder", _
    5. Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)
    6. '========= Now set the value for Folder Dialog using DirectCast
    7. '=== 18 = Network Neighborhood is the root
    8. fieldInfo.SetValue(fbd, DirectCast(18, Environment.SpecialFolder))
    9. fbd.ShowDialog()

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: FolderBrowserDialog

    You know, you would think they would just add the stupid flags in there for you to that enum... Since they don't you have to get a little creative.

    Since you are using VS2008, you can use extension methods, to extend functionality of existing classes.

    So lets make an extension method to add a SetRootFolder sub routine to the folderbrowsedialog class.

    You do this by adding a module to your project, then add this code:

    Code:
    Imports System
    Imports System.Reflection
    Imports System.Runtime.CompilerServices
    
    Module FolderBrowserExtensions
    
        <Extension()> _
        Public Sub SetRootFolder(ByVal fbd As System.Windows.Forms.FolderBrowserDialog, ByVal SpecialFolder As SpecialFoldersEx)
            Dim t As Type = fbd.[GetType]()
            Dim fi As FieldInfo = t.GetField("rootFolder", BindingFlags.Instance Or BindingFlags.NonPublic)
            fi.SetValue(fbd, DirectCast(SpecialFolder, System.Environment.SpecialFolder))
        End Sub
    
        <Flags()> _
        Public Enum SpecialFoldersEx
            Desktop = &H0
            ' Desktop
            Internet = &H1
            ' Internet Explorer (icon on desktop)
            Programs = &H2
            ' Start Menu\Programs
            Controls = &H3
            ' My Computer\Control Panel
            Printers = &H4
            ' My Computer\Printers
            Personal = &H5
            ' My Documents
            Favorites = &H6
            ' user name\Favorites
            Startup = &H7
            ' Start Menu\Programs\Startup
            Recent = &H8
            ' user name\Recent
            SendTo = &H9
            ' user name\SendTo
            BitBucket = &HA
            ' desktop\Recycle Bin
            StartMenu = &HB
            ' user name\Start Menu
            MyDocuments = &HC
            ' logical "My Documents" desktop icon
            MyMusic = &HD
            ' "My Music" folder
            MyVideo = &HE
            ' "My Videos" folder
            DesktopDirectory = &H10
            ' user name\Desktop
            Drives = &H11
            ' My Computer
            Network = &H12
            ' Network Neighborhood (My Network Places)
            Nethood = &H13
            ' user name\nethood
            Fonts = &H14
            ' windows\fonts
            Templates = &H15
            CommonStartMenu = &H16
            ' All Users\Start Menu
            CommonPrograms = &H17
            ' All Users\Start Menu\Programs
            CommonStartup = &H18
            ' All Users\Startup
            CommonDesktopDirectory = &H19
            ' All Users\Desktop
            AppData = &H1A
            ' user name\Application Data
            PrintHood = &H1B
            ' user name\PrintHood
            LocalAppData = &H1C
            ' user name\Local Settings\Applicaiton Data (non roaming)
            AltStartup = &H1D
            ' non localized startup
            CommonAltStartup = &H1E
            ' non localized common startup
            CommonFavorites = &H1F
            InternetCache = &H20
            Cookies = &H21
            History = &H22
            CommonAppdata = &H23
            ' All Users\Application Data
            Windows = &H24
            ' GetWindowsDirectory()
            System = &H25
            ' GetSystemDirectory()
            ProgramFiles = &H26
            ' C:\Program Files
            MyPictures = &H27
            ' C:\Program Files\My Pictures
            Profile = &H28
            ' USERPROFILE
            SystemX86 = &H29
            ' x86 system directory on RISC
            ProgramFilesX86 = &H2A
            ' x86 C:\Program Files on RISC
            ProgramFilesCommon = &H2B
            ' C:\Program Files\Common
            ProgramFilesCommonx86 = &H2C
            ' x86 Program Files\Common on RISC
            CommonTemplates = &H2D
            ' All Users\Templates
            CommonDocuments = &H2E
            ' All Users\Documents
            CommonAdminTools = &H2F
            ' All Users\Start Menu\Programs\Administrative Tools
            AdminTools = &H30
            ' user name\Start Menu\Programs\Administrative Tools
            Connections = &H31
            ' Network and Dial-up Connections
            CommonMusic = &H35
            ' All Users\My Music
            CommonPictures = &H36
            ' All Users\My Pictures
            CommonVideo = &H37
            ' All Users\My Video
            Resources = &H38
            ' Resource Direcotry
            ResourcesLocalized = &H39
            ' Localized Resource Direcotry
            CommonOemLinks = &H3A
            ' Links to All Users OEM specific apps
            CdBurnArea = &H3B
            ' USERPROFILE\Local Settings\Application Data\Microsoft\CD Burning
            ComputersNearMe = &H3D
            ' Computers Near Me (computered from Workgroup membership)
            FlagCreate = &H8000
            ' combine with CSIDL_ value to force folder creation in SHGetFolderPath()
            FlagDontVerify = &H4000
            ' combine with CSIDL_ value to return an unverified folder path
            FlagNoAlias = &H1000
            ' combine with CSIDL_ value to insure non-alias versions of the pidl
            FlagPerUserInit = &H800
            ' combine with CSIDL_ value to indicate per-user init (eg. upgrade)
            FlagMask = &HFF00
        End Enum
    
    End Module

    Then in your form, you can simple use this:

    Code:
            Using FBD As New FolderBrowserDialog
                FBD.SetRootFolder(SpecialFoldersEx.Network)
                FBD.ShowDialog(Me)
                'MORE CODE HERE
            End Using

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: FolderBrowserDialog

    considering it looks like Paul and I came up with the exact same solution (just implemented differently), and I found the reflection trick via google. I will assume he did too

    So just as a side tip, most answers are already on the web

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: FolderBrowserDialog

    yep. you caught me. i googled it

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: FolderBrowserDialog

    no harm in that of course. It just goes to show really that many answers are already available, and its a matter of knowing how to look for them. I googled it too.

    It is not like I never have questions and know all things about programming. I just tend to not need to ask questions on here because I usually find my answer before it gets to the point of needing to ask.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Location
    Norway
    Posts
    24

    Re: FolderBrowserDialog

    Thanks guys.

    This is exactly what i was looking for

    I also found this :
    http://www.codeproject.com/KB/dotnet...k_Folders.aspx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width