Change in AppSettings needs restart my Application how can I avoid? - c#

I'm using C# .NET 2.0 Windows Application.
and I'm using app.config for my Application Settings.
but change in AppSettings doesn't reflected runtime, it Needs Application to be restarted.
How can I avoid it.
Here is my code snippet I used to read and write the Application Settings.
I'm reading the Setting like this
string temp = ConfigurationManager.AppSettings.Get(key);
I'm updating the value like this where node is the current configuration/appSettings Node
node.Attributes["value"].Value = value;
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

You could try calling ConfigurationManager.RefreshSection("appSettings") to refresh the AppSettings section of the file from disk. Once they have been refreshed, you should be able to read the new values.
I've just tested this and it does indeed work.

Alternatively, you could create a singleton 'Options' to hold on to your application settings and perform your read/writes for you. Once loaded, changing the .config doesn't require reloading, you simply set a property on the singleton and call your .Save() method.
The 'runtime' version of your settings is in the singleton, no need to read from disk.

Dont use ConfigurationManager to read settings, instead use:
System.Configuration.ConfigurationManager.OpenExeConfiguration(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile).AppSettings.Settings["value"];

ConfigurationManager.RefreshSection("appSettings");
works!!
But be careful that if we are in debug mode, the configuration file can be called xxxxx.vshost.exe.config, where xxxxx is your project name.

Related

Is it possible to serialize changes in your application configuration to your application's app.config file?

Is there a built-in mechanism for serializing changes to your app.config file using built-in functions available in .NET?
For instance, if I have a custom property set in Executable.exe.config that changes during runtime, I would like .NET to update the Executable.exe.config accordingly.
I know I could do this by creating my own serialization mechanism, but would like to know if it's possible using functionality already available in .NET by default.
You can use the ConfigurationManager class.
There are a lot of sample on the link above or on this article that also provides a short sample.. You need to add a reference to System.Configuration, and basically, you access and save the file like this:
// Open App.Config of executable
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Do stuff
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
In order to force your program to immediatly take the refreshed data, you need to refresh the section using the ConfigurationManager.RefreshSection method
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
And an important note from this thread, for those that say that it doesn't work in debug mode:
If you are running the code from the debugger (within VS) than your
code is actually changing the YourAssemblyName.vshost.exe.Config If
you start YourAssemblyName .exe directly from the bin\debug folder it
will change YourAssemblyName.exe.Config

Overwrite app.config with the default one

How can I overwrite the app.config file form the App Data folder with the default one by code?
Not sure what you mean but you can use at the configurationSettings class to manage settings, you can also use a simple Data.XML namespace and change the XML in the config file and save it back but just beware that it is not the recommanded.
typically i have always created an config object that i read all the config file settings and use it as a singleton in my code. if you want you can add a save method to the class that saves back the config file if you want to persist the changes that your code makes to the config class.
You can use a settings file. The VS designer will generate a proxy class for you. For each entry you can specify in the properties window wether you want to "GenerateDefaultInCode" or not. This way, you can configure your settings in the app.config file under the "ApplicationsSettings" section and at the same time rely on default values (set via attributes in the proxy class) in the generated code.
See http://msdn.microsoft.com/en-us/library/aa730869(VS.80).aspx for how to add a .settings file to your project.
As pointed out here default values should be used with care because they can lead to error-prone behaviour when you deploy to a different environment.

How can I get ConfigurationManager to load application settings from multiple files?

I'm writing applications that interoperate with a third-party application. This application exposes an API to developers via methods in a DLL. Some time ago, the vendor of this app started integrating their own .NET components into their program, and when they did, they decided that their components should use the ConfigurationManager to get settings at runtime.
What this means: their program, foo.exe, calls fooengine.dll, and it reads its settings from foo.exe.config. My program, bar.exe, also calls fooengine.dll, and it reads its settings from bar.exe.config.
Well, that's just plain wrong. But how do I fix it?
The simple workaround is to replicate foo.exe.config's settings in bar.exe.config. That'll work, but it's stupid. It means that from an administrative standpoint, a given setting has to be maintained in N different files. That's going to fail sooner or later.
I tried putting a configSource attribute on the appSettings section in my config file. (As it happens, I'm using the applicationSettings section for my settings, and they're using the appSettings section for theirs, so I can live with simply getting that section from a different file.) But the ConfigurationManager doesn't like that: it wants the path in configSource to be not only relative to but below my program's directory.
I can physically read their settings file into an XmlDocument and then set them myself. But now I'm tightly coupling my code to their implementation; if they put out a new release that moves the settings to the applicationSettings section (which is where they should be now that it's 2009), my code will break.
Is there another way out of this?
Okay, I think I've found the answer, at least for my specific version of this problem.
The .NET 2.0 ConfigurationManager supports a file attribute on the appSettings element. This lets you get the contents of that element from an external file. So what I do is:
Cut the appSettings element out of foo.exe.config and paste it into another file in that directory called, let's say, appSettings.xml.
Add a new element to foo.exe.config: <appSettings file="appSettings.xml"/>.
Add an element to bar.exe.config: <appSettings file="c:\program files\foo\appSettings.xml"/>
This works. But it only works because my program doesn't use appSettings at all.
You could just load their configuration file.

How to change in runtime application settings

I'm trying to change in runtime one key of my applications settings file, but it does not work.
I do on that way:
ConfigurationSettings.AppSettings["XPTO"] = "HELLO";
It seems that it only changes in memory, not on the file.
Does anyone knows how to do this?
Thanks.
Take a look at my overview of .NET settings files...In short, I think you want a user-scoped setting. It will behave more like you expect.
Edit: If you are using the settings designer in Visual Studio, then simply change the "Scope" to "User". If not, you should be able to do the equivalent programmatically.
Assuming your app has write permissions on the file...
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // the config that applies to all users
AppSettingsSection appSettings = config.AppSettings;
if (appSettings.IsReadOnly() == false)
{
appSettings("Key").Value = "new value";
config.Save();
}
I'm ignoring all the possible exceptions that can be thrown...
The AppSettings file is not designed to be writable. It is designed to store configurations that will not change at run time but might change over time ie: DB Connection Strings, web service URL's, etc.
So, while it may be possible to update the file in reality you should re-asses if this value should be stored there.

How can I store user-tweakable configuration in app.config?

I know it is a good idea to store configuration data in app.config (e.g. database connection strings) instead of hardcoing it, even if I am writing an application just for myself. But is there a way to update the configuration data stored in app.config from the program that is using it?
If you use the Settings for the project, you can mark each setting as either application or user.
If they're set as user, they will be stored per-user and when you call the Save method it will be updated in the config for that user.
Code project has a really detailed article on saving all types of settings.
app.config isn't what you want to use for user-tweakable data, as it'll be stored somewhere in Program Files (which the user shouldn't have write permissions to). Instead, settings marked with a UserScopedSettingAttribute will end up in a user-scoped .config file somewhere in %LocalAppData%.
I found the best way to learn this stuff was to mess with the Visual Studio "Settings" tab (on your project's property pages), then look at the code that it generates and look in %LocalAppData% to see the file that it generates.

Categories