Title: List the locations of special folders in C#
The System.Environment class's SpecialFolders enumeration lists special folders such as System, Cookies, Desktop, and so forth. The class's GetFolderPath method returns the full path for one of the special folders values.
This program uses the following code to enumerate the Environment.SpecialFolders values and call the DescribeFolder method for each of them.
// List the folder types.
private void Form1_Load(object sender, EventArgs e)
{
foreach (Environment.SpecialFolder folder_type
in Enum.GetValues(typeof(Environment.SpecialFolder)))
{
DescribeFolder(folder_type);
}
txtFolders.Select(0, 0);
}
The following code shows the DescribeFolder method.
// Add a folder's information to the txtFolders TextBox.
private void DescribeFolder(Environment.SpecialFolder folder_type)
{
txtFolders.AppendText(
String.Format("{0,-25}", folder_type.ToString()) +
Environment.GetFolderPath(folder_type) + "\r\n");
}
The DescribeFolder method adds text to the txtFolders TextBox. It uses String.Format to display the folder type converted into a string and padded on the right to 25 spaces. It uses the Environment.GetFolderPath method to add the folder's path to the text and finishes the folder's entry with a new line.
Download the example to experiment with it and to see additional details.
|