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 folder values.

This program uses the following code to enumerate the System.Environment.SpecialFolders values and call the DescribeFolder method for each.

// List the folder types.
private void Form1_Load(object sender, EventArgs e)
{
    foreach (System.Environment.SpecialFolder folder_type
        in Enum.GetValues(typeof(System.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(System.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 to 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.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments

Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.