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

I have a situation where I run multiple instances of my WPF application. I want instances to use the same user.config file. Currently, which instance writes the latest wins to the user.config file. I would prefer that all instances look at 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 to get the full path (including the hash) to add an observer to the user.config file?

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

Properties.Settings.Default.Reload();
+3
source share
3 answers

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

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

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

+6
source

CacheDependency, - . XML , , . :

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

.

+1

Can I use the fileSystemWatcher control?

it has a modified event that you can trigger

0
source

All Articles