.NET: Setting the RootFolder to Other Values in the FolderBrowserDialog in .NET


Previous Topic Previous Next Topic Next
Xoc Software
Training
RVBA Conventions
Maya Calendar Program
Company Information
Tools
ASP.NET and Other Tips
.NET: Debugging Designer Features of a Custom Control in Visual Studio
.NET: Setting the Default Font in a Windows Mobile/Compact Framework Custom Control
.NET Fixing C# XML Comments so they Work in Windows XP SP2
.NET: Getting and Setting the Application Version Number
.NET: Getting the Path of the Executing Assembly
.NET: Retrieving Assembly Attributes
.NET: Setting the RootFolder to Other Values in the FolderBrowserDialog in .NET
.NET: Sizing Columns in a ListView Control in .NET
.NET: Using Remoting in .NET
ASP.NET: Constructing a Graphic on the Fly in ASP.NET
ASP.NET: Controlling Caching in ASP.NET Web Forms
ASP.NET: How to use the FrontPage Server Extensions with ASP.NET
ASP.NET: Seeing What is in the ViewState in ASP.NET Web Forms
ASP.NET: Using Forms Authentication in ASP.NET
ASP.NET: View Trace Information on your ASP.NET Web Pages
ASP: Create XML from an ADO query
ASP: Detect Incomplete Loads
ASP: Including an ASP.NET Web Page In a Classic ASP Web Page
ASP: Process .HTM Files with Scripts and Server Side Includes
ASP: QuickSort Algorithm
ASP: Retrieve all server variables from IIS
ASP: Send Email from Active Server Page
HTML: How to Create a Non-Scrolling Region in HTML
IE: Allowing Only Certain ActiveX Controls to Run in Internet Explorer
IIS: Creating a web site for testing in IIS Server
IIS: Creating Multiple Web Sites within IIS on Windows 2000 and Windows XP Professional
IIS: IIS/Visual InterDev Problems and Fixes
IIS: Redirect a domain such as xoc.net to www.xoc.net
SQL Server: Execute SQL Server Updategram
Web Design: Design for People with Disabilities
Web Design: Keep a Web Page out of the Google Cache
Windows: Get HTTP Header of a Web Page using Telnet
Windows: Testing Domain Names without DNS
Windows: Using Hosts File to Access Web Sites with XP SP2
Windows: Windows XP Command Line Tools
Windows Mobile: Reprogramming the Push-to-Talk Button on the AT&T Tilt
Articles
Miscellaneous
Downloads
Links
Search
Email

Other Xoc managed sites:
http://grr.xoc.net
http://www.986faq.com
http://www.mayainfo.org
https://mayacalendar.xoc.net
http://www.yachtslog.com

The Browser Folder Dialog in ASP.NET 1.1 allows you to select a folder. There is a property, RootFolder, that allows you to select what folder is the root in the dialog. The RootFolder property can be set to any of the values in the System.Environment.SpecialFolder enum in the .NET Framework. However, there may be settings that are not in that enumeration that you may want to use. For example, you may want to set the root to be My Network Places.

It turns out that the Browse Folder Dialog is a cover for a Windows API call. The Windows API call takes a parameter that is a constant that indicates what the root should be. For some reason the allowed constants in Windows were not fully implemented in the System.Environment.SpecialFolder enum (for no good reason that I can figure out).

You can't just set the RootFolder property to the values in the constants provided by Windows. Instead you will have to pull a trick using Reflection to modify the internally stored number in a private variable inside the dialog.

Include the following class in your project:

[c#]

using System;
using System.Reflection;

public sealed class FolderBrowserDialogEx
{
    [Flags()]
    public enum CsIdl
    {
        Desktop =                   0x0000, // Desktop
        Internet =                  0x0001, // Internet Explorer (icon on desktop)
        Programs =                  0x0002, // Start Menu\Programs
        Controls =                  0x0003, // My Computer\Control Panel
        Printers =                  0x0004, // My Computer\Printers
        Personal =                  0x0005, // My Documents
        Favorites =                 0x0006, // user name\Favorites
        Startup =                   0x0007, // Start Menu\Programs\Startup
        Recent =                    0x0008, // user name\Recent
        SendTo =                    0x0009, // user name\SendTo
        BitBucket =                 0x000a, // desktop\Recycle Bin
        StartMenu =                 0x000b, // user name\Start Menu
        MyDocuments =               0x000c, // logical "My Documents" desktop icon
        MyMusic =                   0x000d, // "My Music" folder
        MyVideo =                   0x000e, // "My Videos" folder
        DesktopDirectory =          0x0010, // user name\Desktop
        Drives =                    0x0011, // My Computer
        Network =                   0x0012, // Network Neighborhood (My Network Places)
        Nethood =                   0x0013, // user name\nethood
        Fonts =                     0x0014, // windows\fonts
        Templates =                 0x0015,
        CommonStartMenu =           0x0016, // All Users\Start Menu
        CommonPrograms =            0x0017, // All Users\Start Menu\Programs
        CommonStartup =             0x0018, // All Users\Startup
        CommonDesktopDirectory =    0x0019, // All Users\Desktop
        AppData =                   0x001a, // user name\Application Data
        PrintHood =                 0x001b, // user name\PrintHood
        LocalAppData =              0x001c, // user name\Local Settings\Applicaiton Data (non roaming)
        AltStartup =                0x001d, // non localized startup
        CommonAltStartup =          0x001e, // non localized common startup
        CommonFavorites =           0x001f,
        InternetCache =             0x0020,
        Cookies =                   0x0021,
        History =                   0x0022,
        CommonAppdata =             0x0023, // All Users\Application Data
        Windows =                   0x0024, // GetWindowsDirectory()
        System =                    0x0025, // GetSystemDirectory()
        ProgramFiles =              0x0026, // C:\Program Files
        MyPictures =                0x0027, // C:\Program Files\My Pictures
        Profile =                   0x0028, // USERPROFILE
        SystemX86 =                 0x0029, // x86 system directory on RISC
        ProgramFilesX86 =           0x002a, // x86 C:\Program Files on RISC
        ProgramFilesCommon =        0x002b, // C:\Program Files\Common
        ProgramFilesCommonx86 =     0x002c, // x86 Program Files\Common on RISC
        CommonTemplates =           0x002d, // All Users\Templates
        CommonDocuments =           0x002e, // All Users\Documents
        CommonAdminTools =          0x002f, // All Users\Start Menu\Programs\Administrative Tools
        AdminTools =                0x0030, // user name\Start Menu\Programs\Administrative Tools
        Connections =               0x0031, // Network and Dial-up Connections
        CommonMusic =               0x0035, // All Users\My Music
        CommonPictures =            0x0036, // All Users\My Pictures
        CommonVideo =               0x0037, // All Users\My Video
        Resources =                 0x0038, // Resource Direcotry
        ResourcesLocalized =        0x0039, // Localized Resource Direcotry
        CommonOemLinks =            0x003a, // Links to All Users OEM specific apps
        CdBurnArea =                0x003b, // USERPROFILE\Local Settings\Application Data\Microsoft\CD Burning
        ComputersNearMe =           0x003d, // Computers Near Me (computered from Workgroup membership)
        FlagCreate =                0x8000, // combine with CSIDL_ value to force folder creation in SHGetFolderPath()
        FlagDontVerify =            0x4000, // combine with CSIDL_ value to return an unverified folder path
        FlagNoAlias =               0x1000, // combine with CSIDL_ value to insure non-alias versions of the pidl
        FlagPerUserInit =           0x0800, // combine with CSIDL_ value to indicate per-user init (eg. upgrade)
        FlagMask =                  0xFF00, // mask for all possible flag values
    }
        
    private FolderBrowserDialogEx()
    {
    }

    public static void SetRootFolder(System.Windows.Forms.FolderBrowserDialog fbd, CsIdl csidl)
    {
        Type t = fbd.GetType();
        FieldInfo fi = t.GetField("rootFolder", BindingFlags.Instance | BindingFlags.NonPublic);
        fi.SetValue(fbd, (System.Environment.SpecialFolder) csidl);
    }
}

To set the FolderRoot property, call it like this:

[c#]

FolderBrowserDialogEx.SetRootFolder(fbd, FolderBrowserDialogEx.CsIdl.Network);

Where fbd is the name of the FolderBrowserDialog on your form, and the second argument is what you want to use as the root of the dialog.


Top