views:

357

answers:

2

Is there a way of converting special folder paths to a full file name (and back) or do I need to code my own (not hard I know, but no point if it exists)

e.g. I want to store the file name of a template for an application, which the user can then change, it exists in the LocalApplicationData folder.

what I would like to store is the location of the file in the format:

%LOCALAPPDATA%\MyApp\Templates\Report Template.xls

so that this application file can be used by many users, each user when they open it will get the Report Template from their own local app directory.

I can write

replace("%LOCALAPPDATA%", _
    System.Environment.GetFolderPath(
         System.Environment.SpecialFolder.LocalApplicationData))

and vice versa

when I come to save the file location, however is there a System.IO (or similar) call to do this for me, rather than having to go through every possible special folder?

+3  A: 

Look at: Environment.ExpandEnvironmentVariables

After some looking around I don't think there is a built-in way available to convert it back, though.

You can do this though:

static void Main(string[] args)
{
    var values = Enum.GetValues(typeof(Environment.SpecialFolder));

    foreach (Environment.SpecialFolder value in values)
        Console.WriteLine(value + " : " + Environment.GetFolderPath(value));

    Console.ReadKey();
}
chakrit
ah... I really shouldn't be on SO without a cup of coffee in the morning.
Eoin Campbell
Thanks, I did try this first, however Whilst this works with Visa, since items like %LOCALAPPDATA% are set, it doesn't work with XP where these special folders are not set as environment variables!
GalleySlave
I think that is another issue entirely.
chakrit
A: 

The normal Windows way to identify "special folders" is by their CSIDL. Environment.SpecialFolder is just a small wrapper around it. As you noted, in a comment to chakrit's post, most CSIDLs simply do not have corresponding environment variables. This is a likely reason why there is no function to find the environment variable for the few CSIDLs that do.

MSalters