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.
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?