简体   繁体   中英

How can I watch the user.config file and reload the settings when it changes?

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

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.

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.

could you use the fileSystemWatcher control?

it has a modified event you can trigger

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM