Here’s a 2-fer! Enumerating enums, and listing special folders

This is a blog mainly for future-me, because I never seem to remember the technique/syntax exactly on how to do this. Although, maybe this will also help others.

I wanted to loop through all of the values of an enum, show the string-version of the enum, and use that enum. See the following code:

string[] names = Enum.GetNames(typeof(Environment.SpecialFolder));
foreach (string name in names)
    Response.Write("Environment.GetFolderPath(Environment.SpecialFolder." + name + ")t"
        + Environment.GetFolderPath(
                         typeof(Environment.SpecialFolder), name)) + Environment.NewLine);

So the first part is getting an array of the string version of the enum names. You use Enum.GetNames() to do that. The second part is taking a string-value, and parsing/translating it into an actual enum value. You use Enum.Parse() for that.

This example shows how extract the string names of an enum, and then take those string names, and “parse” them back into strongly-typed enums. Hope this helps you, Future-Robert!

Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.


Enter your email address to follow this blog and receive notifications of new posts by email.

Join 9 other followers

%d bloggers like this: