
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 Environment.SpecialFolder enum 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.
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 some common values of Environment.SpecialFolder.
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.
Environment.GetFolderPath Enum ExamplesFragment that uses Environment.SpecialFolder [C#]
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. 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.
OpenFileDialog TutorialThe 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. I suggest you use the Settings.settings file to store the folder location for the next startup.
Settings.settings in Visual StudioFragment that uses Environment.GetFolderPath [C#]
private void TestMethod()
{
//
// Store the location of Pictures folder in a string.
//
Environment.SpecialFolder special = Environment.SpecialFolder.MyPictures;
string folderLocation = Environment.GetFolderPath(special);
}We saw how you can use Environment.SpecialFolder, which vastly improves 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. You should use Environment.SpecialFolder to easily initialize the folder location to a normal place where your user won't be confused.
.NET Framework Info