C#Dot Net Perls

C#
Environment SpecialFolder Use

by Sam Allen
Special folders in Vista.

Problem

Use special folders in our Windows Forms programs. On Windows, users put most of their files in special folders, and programs use them extensively. For example, Microsoft Word will save to Documents, and Windows Media Player will use the Music folder. We need a way to reliably access these special directories.

C# Solution

We will use an enum type. In the screenshot on this article, you can see 4 special folders. To find the locations of these folders, we need the Environment.­SpecialFolder enumeration, and combine it with another method. Here is a table listing some common values of Environment.­SpecialFolder.

SpecialFolder enum Folder description
Desktop The Desktop--use this for saving a file to the desktop
Programs Where programs are stored in Windows
MyComputer Probably not useful very often
MyMusic Useful for Windows Media files
MyPictures Used by Adobe Photoshop and ideal for graphics apps
StartMenu The Start Menu is actually a folder and this is its location
Startup Could be useful if you need to know if the user has a program running at startup
DesktopDirectory Just another path to the first Desktop enum
MyDocuments Used by Microsoft Word and others for documents

How can I use the SpecialFolder?

First, let me remind you of IntelliSense, which lets you type in Environment.SpecialFolder, press period and then get a neat list of all the values. For our example, I will use MyDocuments. This is an ideal place for boring business stuff. Now, the SpecialFolder enum isn't worth a lot as just an enum--we need to combine it with GetFolderPath.

// A Windows Form called ExampleForm.
public partial class ExampleForm : Form
{
    // Initialize the controls on the form in the constructor here.
    public ExampleForm()
    {
        InitializeComponent();

        // Environment.GetFolderPath turns the special folder enum into a real string.
        // InitialDirectory is a string property, and we assign it to the string
        // where My Documents is located.
        openFileDialog1.InitialDirectory =
            Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    }
}

The above example will initialize the open file dialog's location to My Documents, or in Vista just Documents. This means the user won't be shown some strange file system location. He will thank you for not causing him another headache.

Here's another example

The Environment.GetFolderPath pattern is not at all restricted to use with InitialDirectory. GetFolderPath simply converts the enum into a usable string by the system. You can assign it like I do in the following example if you want to.

private void TestMethod()
{
    // Store location of Pictures folder in a string.
    Environment.SpecialFolder special = Environment.SpecialFolder.MyPictures;
    string folderLocation = Environment.GetFolderPath(special);
}

Conclusion

This subject is important because it can vastly improve the usability of your program. I have been frustrated so many times when a program starts the open file dialog location in some strange place. Use Environment.SpecialFolder to easily initialize the folder location to a normal place where your user won't be confused.

Dot Net Perls is dedicated to sharing code and knowledge. It has
© 2007-2008 Sam Allen. All rights reserved.

Ads by The Lounge