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?
javajawa
August 1st, 2008, 07:59 AM
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.
javajawa
August 1st, 2008, 01:51 PM
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. :thumbd:)
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:
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
HanneSThEGreaT
August 2nd, 2008, 04:40 AM
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 :
::{450d8fba-ad25-11d0-98a8-0800361b1103}
My Computer Looks like :
::{20D04FE0-3AEA-1069-A2D8-08002B30309D}
Recycle Bin, looks like :
::{645FF040-5081-101B-9F08-00AA002F954E}
Control Panel will look like :
::{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.
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.
javajawa
August 2nd, 2008, 06:27 AM
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?
Shuja Ali
August 4th, 2008, 04:51 AM
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.
HanneSThEGreaT
August 15th, 2008, 01:33 AM
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.
:thumb:
Do I sense a discriprency here?
:D
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 :
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 :
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 :)
javajawa
August 15th, 2008, 03:41 AM
You know, I'll just take your word for it :D. There's enough code there to make it look likely!
HanneSThEGreaT
August 15th, 2008, 04:11 AM
For the interested members & guests, here is the zip, as explained in my previous post :)
auxsys
February 7th, 2009, 10:34 PM
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.
dglienna
February 8th, 2009, 06:53 AM
Did you miss this part?
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)
auxsys
February 8th, 2009, 01:27 PM
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!
zitu
January 27th, 2011, 08:03 AM
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.
zitu
January 27th, 2011, 08:42 AM
I've found a simple workaround:
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
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.