Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place a Windows special folder in config file

Tags:

c#

.net

In my c# application I have a custom config section to determine where to store and retrieve certain files. However the default location for these is in the c:\ProgramData directory and we access it by using

Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

How can I prepopulate that directory in my config file without having to write out the raw string? It could also be a different special folder in some cases as well.

Basically it is a configuration element that looks like:

<searchpath path="" priority="" isfullpath=""/>

Each one of those is a path to search for configuration profiles for a product however over the course of different versions that directory has been moved around, but for backwards compatibility I still need to search the older paths if I can't find them in the newer paths.

For example I have to search in the product install directory then the ProgramData directory now as our most recent version moved it into the current products install directory, but our older version was storing it in the ProgramData directory.

like image 515
twreid Avatar asked Aug 06 '13 14:08

twreid


2 Answers

How about:

  //string folderKey = ConfigurationManager.AppSettings["Folder"];
  string folderKey = "%CommonApplicationData%\\Test";
  var regex = new Regex("([%][^%]+[%])");
  string folder = regex.Replace(folderKey, (match) => {
    // get rid of %%
    string value = match.Value.Substring(1, match.Value.Length - 2);
    var specialFolder = (Environment.SpecialFolder)Enum.Parse(typeof(Environment.SpecialFolder), value, true);
    return Environment.GetFolderPath(specialFolder);
  });

You can use %% to specify special folder inside your path, or use the value as ordinary path.

like image 104
Ondrej Svejdar Avatar answered Oct 21 '22 01:10

Ondrej Svejdar


Okay, so one approach might be to add an <appSettings> section in your app.config:

<appSettings>
  <add key="programData" value="" />
</appSettings>

and then in the application, build a static class that's used to retrieve the path:

public static class ProgramData
{
    private static string _path;
    public static string Path
    {
        get
        {
            if (!string.IsNullOrEmpty(_path)) { return _path; }

            // let's set it then
            _path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["programData"]))
            {
                _path = ConfigurationManager.AppSettings["programData"];
            }
        }
    }
}

Now when you want the path, all you do is:

var path = ProgramData.Path

Now, if there is an attribute in there and it has a value, it will use that. Otherwise it will default to the SpecialFolder.

One more note, you'll need to add a reference to System.Configuration.

like image 41
Mike Perrenoud Avatar answered Oct 21 '22 02:10

Mike Perrenoud