change App.config  
Author Message
harikrk





PostPosted: .NET Base Class Library, change App.config Top

In my c# desktop application i want to change key in my app.config file at runtime.
How can i change this value in runtime
Regrads
Hari



.NET Development5  
 
 
ahmedilyas





PostPosted: .NET Base Class Library, change App.config Top

if I understand correctly, you have an application settings file and you wish to change a key at runtime. Have you tried this:

System.Configuration.ConfigurationSettings.AppSettings.Set(name, value);

http://msdn2.microsoft.com/en-us/library/system.configuration.configurationsettings.aspx

http://msdn2.microsoft.com/en-us/library/system.configuration.configurationsettings_properties.aspx

http://msdn2.microsoft.com/en-us/library/system.configuration.configurationsettings.appsettings.aspx

however I think doing this approach is actually obsolete in .NET 2.0 and higher.

take a look at this:

http://msdn2.microsoft.com/en-US/library/k4s6c3a0(VS.80).aspx

and follow on how to create application settings and from this you can then set the values you want to for your application settings properties.

sorry I Couldnt have been of further assistance however I hope this helps you in some way



 
 
harikrk





PostPosted: .NET Base Class Library, change App.config Top

Thanks for the support. I tried as you told. But i felt that the scope of the setting i made is only for a single run. I want to retain the change even after i close this application. When i open the application next time i want to get the perivious config. I want to write this setting physically. As app.config is the standard to store these config, i thought it will be easy to write into that.
If this is not possible, How can i get the current running application path, that is where this application will be installed and running from after i instal

 
 
Mark Benningfield





PostPosted: .NET Base Class Library, change App.config Top

Hello All.

harikrk:

If these are settings that you are changing at runtime, then you should use user-scoped settings, since the procedures for reading and writing them at runtime are very straightforward and easy to use. Besides, if it is a setting that changes at runtime, wouldn't you want to keep track of which user made which changes

It is possible to change application-scoped settings at runtime, but it is a REAL pain in the neck!!

Tell us what kind of app you are developing (Windows or Console) and I will walk you through the process of reading and writing user-scoped settings.

HTH.



 
 
Swapna55





PostPosted: .NET Base Class Library, change App.config Top

Hi Mark,

Actually I am also facing the same problem,

But mine is web application, and I need to read in the app.config and then make changes and copy them in to app.config.

Any direction in this greatfull.



 
 
Nitin Badole





PostPosted: .NET Base Class Library, change App.config Top

First set a reference to the following namespace.

Code Snippet

using System.Configuration;


Then...

Code Snippet


Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection configSection = config.AppSettings;


try
{

if (configSection != null)
{
if (configSection.IsReadOnly() == false && configSection.SectionInformation.IsLocked == false)
{
configSection.Settings("KeyName").Value = "NewValue";
config.Save();
}
}
}
catch (ConfigurationException ex)
{
MessageBox.Show(ex.Message, "Configuration Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}


Hope this helps.