CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Oct 2003
    Location
    The Dutch Mountains
    Posts
    125

    SpecialFolder in FolderBrowserDialog

    Hej guys,
    is there away to create a new environment.specialfolder to a self created folder?
    So I can use this as a rootfolder in the FolderBrowserDialog?
    Give up your guns and face the law!!

  2. #2
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: SpecialFolder in FolderBrowserDialog

    The direct answer to your question is no, as it's an enum. And, unfortunately, the FolderBrowseDialog is not inheritable, so I can't see and easy way to add the functionality.

    Your options are really
    a) Wait for a guru who knows a better answer
    b) use the selected folder attribute, and pray the user is sensible
    c) Create your own control using a treeview and some good coding
    d) Look for a third-party control.
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  3. #3
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: SpecialFolder in FolderBrowserDialog

    Present for you - although I wouldn't use it if you expecting loads of folders (I just made the mistake of asking it to display from C:\ downwards. )
    It wouldn't take much to get it to add only two layer down from the current node, thereby stopping large loading times. Also, i've set it out to have a update rather than rebuild functionality. But it's dinner-time here, and I'm hungry...
    I can't be bothered to zip the project - but it's only a user control with a TreeView. The complete folder thing is stored in each node's tag property; here's the code:
    Code:
    Imports System.IO
    
    Public Class RootFolderBrowse
    
            Private mRoot As String = ""
            Public Property RootFolder() As String
                    Get
                            RootFolder = mRoot
                    End Get
                    Set(ByVal value As String)
                            If Strings.Right(value, 1) = "\" Then
                                    value = Strings.Left(value, Strings.Len(value) - 1)
                            End If
                            mRoot = value
                            Me.RefreshTree()
                    End Set
            End Property
    
            Public Sub RefreshTree()
                    If mRoot <> "" Then
                            If Directory.Exists(mRoot) Then
                                    If TreeView1.Nodes.Count > 0 Then
                                            If CStr(TreeView1.Nodes.Item(0).Tag) = mRoot Then
                                                    'Needs to be replaced with update code
                                                    TreeView1.Nodes.Clear()
                                                    AddNodes(TreeView1.Nodes(TreeView1.Nodes.Add(New TreeNode() With {.Tag = mRoot, .Text = LastDir(mRoot)})), mRoot)
                                            Else
                                                    TreeView1.Nodes.Clear()
                                                    AddNodes(TreeView1.Nodes(TreeView1.Nodes.Add(New TreeNode() With {.Tag = mRoot, .Text = LastDir(mRoot)})), mRoot)
                                            End If
                                    Else
                                            TreeView1.Nodes.Clear()
                                            AddNodes(TreeView1.Nodes(TreeView1.Nodes.Add(New TreeNode() With {.Tag = mRoot, .Text = LastDir(mRoot)})), mRoot)
                                    End If
                            End If
                    End If
            End Sub
    
            Public Sub AddNodes(ByRef Parent As TreeNode, ByVal ParentDir As String)
                    For Each Dir As String In Directory.GetDirectories(ParentDir & "\")
                            AddNodes(Parent.Nodes(Parent.Nodes.Add(New TreeNode() With {.Tag = Dir, .Text = LastDir(Dir)})), Dir)
                    Next
            End Sub
    
            Public Function LastDir(ByVal Path As String) As String
                    If InStrRev(Path, "\") > 0 Then
                            LastDir = Mid(Path, InStrRev(Path, "\") + 1)
                    ElseIf InStrRev(Path, "/") > 0 Then
                            LastDir = Mid(Path, InStrRev(Path, "/") + 1)
                    Else
                            LastDir = Path
                    End If
            End Function
    End Class
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: SpecialFolder in FolderBrowserDialog

    I'm by no means a guru
    But here, you will be dealing with CLSIDs
    Each special folder on the system has a CLSID, for example, My Documents look like :
    Code:
     ::{450d8fba-ad25-11d0-98a8-0800361b1103}
    My Computer Looks like :
    Code:
    ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}
    Recycle Bin, looks like :
    Code:
    ::{645FF040-5081-101B-9F08-00AA002F954E}
    Control Panel will look like :
    Code:
    ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\  ::{21EC2020-3AEA-1069-A2DD-08002B30309D}
    All CLSIDs are stored in the Registry, usually at HKEY_CLASSES_ROOT\CLSID. Some of the special CLSIDS can also be found in the Registry keys where the related namespace extensions are specified as in the key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\ CurrentVersion\Explorer\ControlPanel\NameSpace\ where you can find all CLSIDs related to the Control Panel.

    You may want to check these out :

    http://www.pctools.com/guides/registry/detail/73/

    http://www.codeguru.com/vb/gen/vb_sy...php/c13987__1/

    With the first link, it explains how to to what you want, it looks like it at least

    In the second link, my article, it will give you a bigger background on CLSIDs, and how to open them.

    With the FolderBrowser, you can then just set the InitialDirectory to the CLSID that you want.

  5. #5
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: SpecialFolder in FolderBrowserDialog

    Quote Originally Posted by HanneSThEGreaT
    I'm by no means a guru
    .........
    In the second link, my article, it will give you a bigger background on CLSIDs, and how to open them.
    Do I sense a discriprency here?

    With the FolderBrowser, you can then just set the InitialDirectory to the CLSID that you want.
    But can you set the RootDirectory?
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  6. #6
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: SpecialFolder in FolderBrowserDialog

    A simple solution would be to create your own Form that looks like FolderBrowser dialog. And works only on those folders that you want user access to.

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: SpecialFolder in FolderBrowserDialog

    Quote Originally Posted by Shuja Ali
    A simple solution would be to create your own Form that looks like FolderBrowser dialog. And works only on those folders that you want user access to.


    Quote Originally Posted by javajawa
    Do I sense a discriprency here?


    Quote Originally Posted by javajawa
    But can you set the RootDirectory?
    Yes, you can

    Follow these steps :

    Open VB - LOL!
    Add a FolderBrowserDialog to your form, do not give it another name
    Add a button on your form
    Add a class to your project and name it : NewFolderBrowserDlg
    Add this code inside your newly created class :
    Code:
    Imports System
    Imports System.Reflection
    
    Public NotInheritable Class NewFolderBrowserDlg
        <Flags()> _
          Public Enum CsIdl
            Desktop = 0
            ' Desktop 
            Internet = 1
            ' Internet Explorer (icon on desktop) 
            Programs = 2
            ' Start Menu\Programs 
            Controls = 3
            ' My Computer\Control Panel 
            Printers = 4
            ' My Computer\Printers 
            Personal = 5
            ' My Documents 
            Favorites = 6
            ' user name\Favorites 
            Startup = 7
            ' Start Menu\Programs\Startup 
            Recent = 8
            ' user name\Recent 
            SendTo = 9
            ' user name\SendTo 
            BitBucket = 10
            ' desktop\Recycle Bin 
            StartMenu = 11
            ' user name\Start Menu 
            MyDocuments = 12
            ' logical "My Documents" desktop icon 
            MyMusic = 13
            ' "My Music" folder 
            MyVideo = 14
            ' "My Videos" folder 
            DesktopDirectory = 16
            ' user name\Desktop 
            Drives = 17
            ' My Computer 
            Network = 18
            ' Network Neighborhood (My Network Places) 
            Nethood = 19
            ' user name\nethood 
            Fonts = 20
            ' windows\fonts 
            Templates = 21
            CommonStartMenu = 22
            ' All Users\Start Menu 
            CommonPrograms = 23
            ' All Users\Start Menu\Programs 
            CommonStartup = 24
            ' All Users\Startup 
            CommonDesktopDirectory = 25
            ' All Users\Desktop 
            AppData = 26
            ' user name\Application Data 
            PrintHood = 27
            ' user name\PrintHood 
            LocalAppData = 28
            ' user name\Local Settings\Applicaiton Data (non roaming) 
            AltStartup = 29
            ' non localized startup 
            CommonAltStartup = 30
            ' non localized common startup 
            CommonFavorites = 31
            InternetCache = 32
            Cookies = 33
            History = 34
            CommonAppdata = 35
            ' All Users\Application Data 
            Windows = 36
            ' GetWindowsDirectory() 
            System = 37
            ' GetSystemDirectory() 
            ProgramFiles = 38
            ' C:\Program Files 
            MyPictures = 39
            ' C:\Program Files\My Pictures 
            Profile = 40
            ' USERPROFILE 
            SystemX86 = 41
            ' x86 system directory on RISC 
            ProgramFilesX86 = 42
            ' x86 C:\Program Files on RISC 
            ProgramFilesCommon = 43
            ' C:\Program Files\Common 
            ProgramFilesCommonx86 = 44
            ' x86 Program Files\Common on RISC 
            CommonTemplates = 45
            ' All Users\Templates 
            CommonDocuments = 46
            ' All Users\Documents 
            CommonAdminTools = 47
            ' All Users\Start Menu\Programs\Administrative Tools 
            AdminTools = 48
            ' user name\Start Menu\Programs\Administrative Tools 
            Connections = 49
            ' Network and Dial-up Connections 
            CommonMusic = 53
            ' All Users\My Music 
            CommonPictures = 54
            ' All Users\My Pictures 
            CommonVideo = 55
            ' All Users\My Video 
            Resources = 56
            ' Resource Direcotry 
            ResourcesLocalized = 57
            ' Localized Resource Direcotry 
            CommonOemLinks = 58
            ' Links to All Users OEM specific apps 
            CdBurnArea = 59
            ' USERPROFILE\Local Settings\Application Data\Microsoft\CD Burning 
            ComputersNearMe = 61
            ' Computers Near Me (computered from Workgroup membership) 
            FlagCreate = 32768
            ' combine with CSIDL_ value to force folder creation in SHGetFolderPath() 
            FlagDontVerify = 16384
            ' combine with CSIDL_ value to return an unverified folder path 
            FlagNoAlias = 4096
            ' combine with CSIDL_ value to insure non-alias versions of the pidl 
            FlagPerUserInit = 2048
            ' combine with CSIDL_ value to indicate per-user init (eg. upgrade) 
            FlagMask = 65280
            ' mask for all possible flag values 
        End Enum
    
        Private Sub New()
        End Sub
    
        Public Shared Sub SetNewRoot(ByVal fbd As System.Windows.Forms.FolderBrowserDialog, ByVal csidl As CsIdl)
            Dim fdbt As Type = fbd.[GetType]()
            Dim fdbfi As FieldInfo = fdbt.GetField("rootFolder", BindingFlags.Instance Or BindingFlags.NonPublic)
            fdbfi.SetValue(fbd, DirectCast(csidl, System.Environment.SpecialFolder))
        End Sub
    
    End Class
    On form1, add the following :
    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            NewFolderBrowserDlg.SetNewRoot(FolderBrowserDialog1, NewFolderBrowserDlg.CsIdl.Favorites)
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            FolderBrowserDialog1.ShowDialog()
        End Sub
    Once run, your FolderBrowserDialog, will show the Favourites folder

    If you have added your own CLSID, you could have listed them there as well
    Last edited by HanneSThEGreaT; August 15th, 2008 at 01:42 AM.

  8. #8
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: SpecialFolder in FolderBrowserDialog

    You know, I'll just take your word for it . There's enough code there to make it look likely!
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  9. #9
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: SpecialFolder in FolderBrowserDialog

    For the interested members & guests, here is the zip, as explained in my previous post
    Last edited by HanneSThEGreaT; June 14th, 2010 at 05:40 AM.

  10. #10
    Join Date
    Feb 2009
    Posts
    2

    Re: SpecialFolder in FolderBrowserDialog

    Hi! Sorry for reopen this threat, and sorry for my english... but I'm in the same situation in that the creator of this threat was.
    I've reading the links that HanneSThEGreaT left, but... how can I get a number as 17 from a CLSID displayed like {20D04FE0-3AEA-1069-A2D8-08002B30309D} ?? So, what have I missed?
    Ok, thanks! ...and sorry again for my english.

  11. #11
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: SpecialFolder in FolderBrowserDialog

    Did you miss this part?

    Code:
            FlagCreate = 32768
            ' combine with CSIDL_ value to force folder creation in SHGetFolderPath() 
            FlagDontVerify = 16384
            ' combine with CSIDL_ value to return an unverified folder path 
            FlagNoAlias = 4096
            ' combine with CSIDL_ value to insure non-alias versions of the pidl 
            FlagPerUserInit = 2048
            ' combine with CSIDL_ value to indicate per-user init (eg. upgrade)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  12. #12
    Join Date
    Feb 2009
    Posts
    2

    Re: SpecialFolder in FolderBrowserDialog

    Mmm ok, thanks. But I can't understand how the function SHGetFolderPath() works, I guess I must read more about that. At least now I know where to find. Thanks again!

  13. #13
    Join Date
    Jan 2011
    Posts
    2

    Re: SpecialFolder in FolderBrowserDialog

    Ok, HanneSThEGreaT's example sets the root folder to favourites. But is there any way to set the root folder a custom folder, decided at runtime?

    Thx.

  14. #14
    Join Date
    Jan 2011
    Posts
    2

    Re: SpecialFolder in FolderBrowserDialog

    I've found a simple workaround:

    Code:
    Public Function BrowseToFolder(ByVal title As String, ByVal rootPath As String) As String
            Dim shellType = Type.GetTypeFromProgID("Shell.Application")
            Dim shell = Activator.CreateInstance(shellType)
            Dim folder = shellType.InvokeMember("BrowseForFolder", BindingFlags.InvokeMethod, Nothing, shell, New Object() {0, title, 0, rootPath})
            If folder Is Nothing Then
                Return Nothing
            End If
            ' User clicked cancel
            Dim folderSelf = folder.[GetType]().InvokeMember("Self", BindingFlags.GetProperty, Nothing, folder, Nothing)
            Return TryCast(folderSelf.[GetType]().InvokeMember("Path", BindingFlags.GetProperty, Nothing, folderSelf, Nothing), String)
        End Function

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

Featured