3

I have a situation where I am running multiple instances of my WPF application. I want the instances to share the same user.config file. Currently, whichever instance writes to the user.config file last wins. I'd prefer for all the instances to watch the user.config file and reload the settings whenever another instance changes it. The user.config file is currently located here:

C:\Documents and Settings\username\Local Settings\Application Data\company name\ExeName.exe_StrongName_hash\1.0.0.0

For example, C:\Documents and Settings\usename\Local Settings\Application Data\Company\5kAdCon.exe_StrongName_gxh0g12uyafipnfrslaggvy4vvk01fko\1.0.0.0

Is there a way for me to get this full path (including the hash) so I can add a file watcher to the user.config file?

If so, I want to reload the settings when the file changes. Is it as easy as calling this method?

Properties.Settings.Default.Reload();
John Myczek
  • 12,076
  • 4
  • 31
  • 44
  • Are you fine if an instance's unsaved settings are overwritten by another instance's? Seems to me the user might not expect all of the settings to reset. At least raise a dialog for this. – Szymon Rozga May 19 '09 at 21:01
  • For the most part, yes. I'm try to avoid having it matter which instance is closed last. Right now the settings for the last instance closed are the ones that get saved. – John Myczek May 19 '09 at 21:11

3 Answers3

6

I found it. The following code will return the path to the user.config file. You need to add a reference to System.Configuration.dll

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
string path = config.FilePath;

Now I can use a FileSystemWatcher to get notified when the file changes.

John Myczek
  • 12,076
  • 4
  • 31
  • 44
1

You should Cache the file and implement CacheDependency so that if any change is made to the file the file gets reloaded in the Cache. I am using a permission xml file in my application which gets stored in the cache and reloaded if file gets changed. Here's the code:

protected void Page_Load(object sender, EventArgs e)
{
        XmlDocument permissionsDoc = null;

        if (Cache["Permissions"] == null)
        {
            string path = Server.MapPath("~/XML/Permissions.xml");
            permissionsDoc = new XmlDocument();
            permissionsDoc.Load(Server.MapPath("~/XML/Permissions.xml"));
            Cache.Add("Permissions", permissionsDoc,
                            new CacheDependency(Server.MapPath("~/XML/Permissions.xml")),
                           Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
                    CacheItemPriority.Default, new CacheItemRemovedCallback(ReloadPermissionsCallBack));
        }
        else
        {
            permissionsDoc = (XmlDocument)Cache["Permissions"];
        }
}

private void ReloadPermissionsCallBack(string key, object value, CacheItemRemovedReason reason)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("~/XML/Permissions.xml"));
        Cache.Insert("Permissions", doc ,
                            new CacheDependency(Server.MapPath("~/XML/Permissions.xml")),
                           Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
                    CacheItemPriority.Default, new CacheItemRemovedCallback(ReloadPermissionsCallBack));
    }

Caching will also increase your application performance.

Rashmi Pandit
  • 23,230
  • 17
  • 71
  • 111
  • Something like that might work, but my biggest problem is I don't know the path to the user.config file. I'm using the built in application settings (http://msdn.microsoft.com/en-us/library/a65txexh.aspx) and don't know how to construct the path that MS picked for the user.config directory. It looks like this: C:\Documents and Settings\usename\Local Settings\Application Data\Company\5kAdCon.exe_StrongName_gxh0g12uyafipnfrslaggvy4vvk01fko\1.0.0.0 – John Myczek May 20 '09 at 15:32
0

could you use the fileSystemWatcher control?

it has a modified event you can trigger

Crash893
  • 11,428
  • 21
  • 88
  • 123
  • 2
    Yes, but I don't know how to get the path to the file that I need to watch. The path looks like this: C:\Documents and Settings\usename\Local Settings\Application Data\Company\5kAdCon.exe_StrongName_gxh0g12uyafipnfrslaggvy4vvk01fko\1.0.0.0. Is there a way to construct this path? – John Myczek May 19 '09 at 21:09
  • how does the program know where it is i know you can use environmental variables to find the user name and what not. unless the rest is static then i don't know what the convention is. – Crash893 May 20 '09 at 13:20
  • 1
    Right, that's my problem. I don't know how to get the "_StrongName_gxh0g12uyafipnfrslaggvy4vvk01fko" part of the path. – John Myczek May 20 '09 at 15:33
  • well obvously the program knows somehow i doub't it would be a very eliquent way of doing things but you could just scan everything under and filter only on config.conf – Crash893 May 20 '09 at 16:15