C# Environment.SpecialFolder

You want to use special folders in your Windows Forms program. 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. Here we see how you can use the enumerations in .NET to locate special folders, using the C# language.

  Desktop  
  Use this for saving a file to the desktop.

  Programs  
  This is where program files are stored in Windows.

  MyComputer  
  Probably not useful very often.

  MyMusic  
  Useful for finding or storing Windows Media files.

  MyPictures  
  Used for Adobe Photoshop; ideal for storing images.

  StartMenu  
  The Start menu is actually a folder, and this is its location.

  Startup  
  Use this to place an executable to be run at startup.
  Can also be used to see if the user is running a program.

  DesktopDirectory  
  This is another path to the first Desktop folder enum.

  MyDocuments  
  Used by Microsoft Word to store user-generated data.

Using Environment.SpecialFolder

In every installation of Windows, there are several special folders. To find the locations of these folders, we need the Environment.SpecialFolder enumeration, and combine it with another method. Above, you can see a table listing some common values of Environment.­SpecialFolder.

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

public partial class ExampleForm : Form
{
    // Initialize the controls on the form in the constructor here.
    public ExampleForm()
    {
        InitializeComponent();
        openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    }
}

Description of the above C# code. The above example will initialize the open file dialog's location to My Documents, or in Vista just Documents. On Windows 7, it may initialize to an even different location. This means the user won't be shown some strange file system location.

See OpenFileDialog Tutorial.

Using Pictures folder

The Environment.GetFolderPath pattern is not restricted to usage with InitialDirectory. GetFolderPath simply converts the enum into a usable string by the system. You can assign it like we see in the following example. The author suggests you use the Settings.settings file to store the folder location for the next startup.

See Settings.settings in Visual Studio.

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

Summary

Here we saw how you can use Environment.SpecialFolder, which vastly improve the usability of your program. The author has been frustrated so many times when a program starts the open file dialog location in some strange place. You should use Environment.SpecialFolder to easily initialize the folder location to a normal place where your user won't be confused.

See Enum Overview.

See Windows Forms Content.

© 2007-2010 Sam Allen. All rights reserved.

Dot Net Perls  Sam Allen