Hanny
Hanny

Reputation: 682

Getting special folders with a dynamic folder name

I have an array of folders I'm trying to get the sizes of.

When I get my Documents directory with this method:

        string desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

It works great.

But I need to be able to dynamically input the last portion of that - is that possible?

I'd like to be able to call something like:

        string desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.VARIABLENAME);

Where VARIABLENAME would be a name that I provide (that is an appropriate 'specialfolder' such as 'Favorites' or 'Desktop')

Can this be done?

Upvotes: 1

Views: 369

Answers (1)

L.B
L.B

Reputation: 116108

you can use the string form of your enum...

var dir = (Environment.SpecialFolder)Enum.Parse(typeof(Environment.SpecialFolder), "Desktop");
string desktopFolder = Environment.GetFolderPath(dir);

Environment.SpecialFolder.Desktop.ToString() ==> Desktop

Upvotes: 3

Related Questions