You can change App.config file runtime using C# in your windows application. Here is the code for that,
1: // Open App.Config of executable
2: System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
3: // Add an Application Setting.
4: config.AppSettings.Settings.Remove("LastDateFeesChecked");
5: config.AppSettings.Settings.Add("LastDateFeesChecked", DateTime.Now.ToShortDateString());
6: // Save the configuration file.
7: config.Save(ConfigurationSaveMode.Modified);
8: // Force a reload of a changed section.
9: ConfigurationManager.RefreshSection("appSettings");
Fig – (1) Change App.config file run time using C#
You need to add reference to System.Configuration namespace to make this code running. You can do that by right clicking on project file in solution explorer and select add reference. See the snippiest below,
When you run the application and debug the code you find that it is not working !! You can sse the effect when you run the exe generated in release directory.
Happy Programming !!!!
Hi,I used your code by the past to create a default config file at run time and it worked like a charm. Now I use exactly the same code in an other application and instead of creating ‘myApp.exe.config’ it creates “myApp.config” and it screws everything. Any idea why?
Hi,
I am using asp.net with c#. I want to use some images and files into my project. I create one folder name “Images” in currnet Project. Still i can acsess image by using hard coded path.
There is any solution to porvied virtual path like asp.net .
Please guide and help me.
Thanks and Regards
Here is “somethnig” you might want to know. You have a spelling mistake in your blog’s tagline.
PS – WordPress is slow and horrible.
Lol I kept trying to figure out what the spelling mistake was. He corrected it already? 🙂
Ed Hassinger,
Thank you. I have changed the spelling.
hi,
i am working in .net 2.0 framework and windows application using c# . I have to change the config files at runtime the value is changed but it takes previous key values when opening application asking outside people is modified ur config do u allow to save like that message came.i applied ur code but i am not getting solution
thanks, its working great and solved my problem.
Hi,
I want to create C# project (e.g B) from an existing C# project (e.g) at runtime from a form (A). It will create all prjojects files.
I have generated .cs files, resource files, but not created assembly file ,project files, solution files.
Please give any solution
This is an old posting that kavitha made, however I think it may help others to remember that if you are running in debug mode, every time you restart the debugger, any changes you make will get overwritten with the defaults in your application.
So this code does work great, and works as expected. thank you
“When you run the application and debug the code you find that it is not working !! You can sse the effect when you run the exe generated in release directory.”
I was running in debug mode, and didn’t see the changes being applied to my connectionstrings, nor in the release exe.
After taking a closer look at the last line:
ConfigurationManager.RefreshSection(“appSettings”);
Changing that to:
ConfigurationManager.RefreshSection(“connectionStrings”);
did the trick! Even in debug mode. I now have dynamic connectionstrings thanks to your example code.
Thanks a bunch for providing it!
Thanx mate…..very useful code
Hi Chirag,
I am creating a windows application that loads up a dll and the dll is actually a wpf app. which has its own config. What i have to do is that i want to merge the config of the dll with the EXE, since otherwise the dll will not run since it will not get the appsettings and customSections defined in its app.config.
So i went ahead and made a utility that would merge the configs. After merging the configs via using the XML document, I have to RE-LOAD/ RESTART my windows application again. Is there a way out of this….i mean i would like to changed my app.config and yet still not want ot restart my Application. Please note that i have not used OpenMappedExeConfiguration since if an external dll has a customSection which has an internal handler its gonna blow up, so i have used xMLDocument to read the configs and merge the same.
Any good ideas….. You know me well….. Conchango..!!!!
The App.config file does change in debug mode, remember that the App.config file you are using while running in debug mode is the one in the /bin/Debug folder…
Why are all of your fingers bandaged?
hi
i have written a program that works with SqlServer2005 , now i have published the program but the database doesn’t work when i install program on the other computer , i know that the program doesn’t know the ConnectionString what should i do for it?? and what requirement the program need for publishing until the computer that does not have sqlserver can work with it ?
pleas email me the answer .
Thanks
Hello,
How can I make the app.Config dynamic?
I want to have textbox to send the ServerName, BdName, Username and Password by user choice.
Can you help me?
Nuno
hello!
i have some problem need you help
i use vb.net and after compile to exe, i want to change path reference that no need compile again. so can do or not?
Thanks for fantastic solution.
The code is really good.
It helped me a lot.
Thank you very much.
Hi chirag,
I tried your code, but this is not working. even I tried to add a completely new key (just for testing purpose).
Can you take a look and tell what could be the problem?
I am using Visual Studio 2005.
The code is as follows :
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove(“test”);
config.AppSettings.Settings.Add(“test”, “new”);
config.AppSettings.Settings.Add(“New”, DateTime.Now.ToShortDateString());
config.Save(ConfigurationSaveMode.Full);
ConfigurationManager.RefreshSection(“appSettings”);
the problem is solved. What I was doing that I was debugging the application in visual studio. when I run the application directly, it worked.
I still wonder why it do not work in debug mode.
Only microsoft would know…
hi
i want to about ,how to store value in config file at insallation time. I have one desktop application project, i gave choice to user at installation time to select language in which he want to install our project .i created setup for that and give language selection option now how can i set the value if he select hindi language or for english so that my project run in that language.
thanx
Hi Experts,
I have developed an C# application in VS2008 prof edition. It’s working fine in the same environment. I have made click once setup exe. When we trying to install on XP Sp2/Sp3 .net 3.5 framework PC it insatlled but not running. Error as below :-
Collapse
EventType : clr20r3 P1 : atl_wb.exe P2 : 1.0.0.0 P3 : 4cb3e44b
P4 : atl_wb P5 : 1.0.0.0 P6 : 4cb3e44b P7 : a P8 : 1a
P9 : system.io.filenotfoundexception
Kindly help me please.
— Anil Kumar Bhakta
Hi
The code is really good.
It helped me a lot.
Thank you very much.
its not working what a bullshit
public void update_app_config()
{
string str = ConfigurationSettings.AppSettings[“Sky_Converter_connectionStrings”];
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (str == null)
{
// ConfigurationSettings.AppSettings.Add(“Sky_Converter_connectionStrings”, GlobalSettings.new_config_con_string);
// config.AppSettings.Settings.Remove(“Sky_Converter_connectionStrings”);
config.AppSettings.Settings.Add(“Sky_Converter_connectionStrings”, GlobalSettings.new_config_con_string);
}
else
{
//ConfigurationSettings.AppSettings.Remove(“Sky_Converter_connectionStrings”);
// ConfigurationSettings.AppSettings.Add(“Sky_Converter_connectionStrings”, GlobalSettings.new_config_con_string);
config.AppSettings.Settings.Remove(“Sky_Converter_connectionStrings”);
//config.AppSettings.Settings.Add(“Sky_Converter_connectionStrings”, GlobalSettings.new_config_con_string);
}
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(“appSettings”);
}
Thank you…. it is working..
Actually in my application i need to change timeinterval of timer control .when i give value in textbox present in the windows form the value as default assigned has to be changed and i need to make the value given in the textbox as a default value.
give me reply as soon as possible
Thanks. It is working…
its working fine. but it does not persist the old values next time when we run the application. Any solution?
Its seems a good code, but i failed to understand, where this code should be place and second thing where are connectionStrings details mention like database name, server name etc
Please provide complete code or instruction, so that a newbie like me should also be benfitted, thanks alot
I install the setup in windows 7 with this code but did not change the app.config file I think the reason is no permission to edit the file how can i add the write permission to the file in windows 7 any idea
Thanks chiragrdarji. As per your code i can dynamically change the appsetting key value and it works fine for me…
I have one doubt on this when i check the Appconfig file the value is not getting updated, at the same time in code its taking the updated value….my functionality is working fine…..please let me know from where the application is picking up the updated value and why the Appconfig doesnot showing the updated value?
Thanks,
Senthil
гомненици
Оти бе гомненици?
[…] Source 31.558573 74.352692 Share this:FacebookLike this:LikeBe the first to like this post. […]
Thank you chiragrdarji.
cool example, thanks
Thanks for the code, work just fine!
Thank you for this example, it’s working and was what I was looking for. I don’t understand, however, why there isn’t a “set” function of some kind. It seems like deleting and recreating a setting is more of a hack than a proper approach.
hi all hw r u?
[…] How to change App.config file run time using C# « Tech … – Sep 25, 2008 · You can change App.config file runtime using C# in your windows application. Here is the code for that, […]