Get Special Folder

Edit: if you’ve inherited the code you’ve shown us and need to use it for some reason (instead of the built-in .NET method that appears to do the same thing), you should be able to use it like this:

string path = EnvironmentFolders.GetPath(EnvironmentFolders.SpecialFolders.Fonts);

Having said that, the Environment class has a public method that does nearly the same thing, GetFolderPath:

string path = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);

The reflected code in .NET looks exactly like the code in your class, except it adds two things: it verifies that the parameter value is defined in the enumeration (since you can pass any integer to the method) and it demands that the caller has path discovery permission (a new FileIOPermission). Is it that last requirement you’re trying to work around?

Both methods are static, meaning you access them through the type that contains them, not an instance of that type:

 // Like this
 EnvironmentFolders.GetPath(...);

 // Not this
 EnvironmentFolders folders = new EnvironmentFolders();
 folders.GetPath(...);

See the .NET documentation about Static Classes and Static Class Members for more information.

Leave a Comment