Search code examples
.netwindows-vistawindows-xp

How to convert from Special Folders to full file names


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?


Solution

  • 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();
    }