Home > How To > How To Change App.config At Runtime

How To Change App.config At Runtime

My vote of 2 vnenad24-Feb-09 9:45 vnenad24-Feb-09 9:45 Author's comment about using hard-coded app.config isn't really mistake..


What I would like to do is the following: Create a new app.config in memory that incorporates the config sections from the modules Tell my application to use that new app.config It is used in all three of the above methods.// Determines if a key exists within the App.config public bool KeyExists(string strKey) { XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings"); // Attempt to locate The reason: There exists a class ClientConfigPaths that caches the paths. Furthermore, I can't overwrite the app.config, because the user doesn't have the right to do so. –Daniel Hilgarth May 27 '11 at 10:48 You would be reloading to load this contact form

foreach (XmlNode childNode in appSettingsNode) { if (childNode.Attributes["key"].Value == strKey) childNode.Attributes["value"].Value = newValue; } xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "..\\..\\App.config"); xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); } Deleting Key-Value Pairs The following method will delete an existing key value Application Lifecycle> Running a Business Sales / Marketing Collaboration / Beta Testing Work Issues Design and Architecture ASP.NET JavaScript C / C++ / MFC> ATL / WTL / STL Managed C++/CLI 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 on February 5, 2010 at 6:12 am | Reply Bhushan The code is really good.

How To Change App.config File Runtime Using C#

Anıl Cansu Taner23-Feb-15 0:15 Anıl Cansu Taner23-Feb-15 0:15 Hey , its a great topic but you will face with a problem when you don't have write permission on that config file The important point is the using statement which will dispose(), which I think was your problem. on March 17, 2011 at 4:36 pm | Reply Pradeep Lol I kept trying to figure out what the spelling mistake was. I used this code to test it: Console.WriteLine(ConfigurationManager.AppSettings["SettingA"]); Console.WriteLine(Settings.Default.Setting); var combinedConfig = string.Format(CONFIG2, CONFIG); var tempFileName = Path.GetTempFileName(); using (var writer = new StreamWriter(tempFileName)) { writer.Write(combinedConfig); } using(AppConfig.Change(tempFileName)) { Console.WriteLine(ConfigurationManager.AppSettings["SettingA"]); Console.WriteLine(Settings.Default.Setting);

I have developed a generic class – “ConfigModificator” for the purpose. It contains only three properties. How are there so many species on the space station 'A long way from anywhere V'? Update App Config Programmatically C# As you can see it is much clearer.

Sorry. –David Harris Jan 6 '10 at 23:17 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook After taking a closer look at the last line: ConfigurationManager.RefreshSection("appSettings"); Changing that to: ConfigurationManager.RefreshSection("connectionStrings"); did the trick! When converting dynamic SQL (pivot query) to xml output, why is the first digit of the date converted to unicode? Thanks a bunch for providing it!

How to change .config file at runtime (C# VB.NET code) Download full source code Introduction At times, you may need to modify the configuration files for your application at runtime. How To Change App.config File Dynamically In C# which has its own config. Browse other questions tagged c# .net app-config or ask your own question. Unfortunately, you can’t RefreshSection on a section group, only a section, so you’ll typically have to use the following calls to refresh WCF sections: ConfigurationManager.RefreshSection("system.serviceModel/behaviors"); ConfigurationManager.RefreshSection("system.serviceModel/bindings"); ConfigurationManager.RefreshSection("system.serviceModel/client"); ConfigurationManager.RefreshSection("system.serviceModel/services"); You

Update App.config Appsettings Programmatically C#

class Program { static void Main(string[] args) { UpdateSetting("lang", "Russian"); } private static void UpdateSetting(string key, string value) { Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); configuration.AppSettings.Settings[key].Value = value; configuration.Save(); ConfigurationManager.RefreshSection("appSettings"); } } From At worst case something with a one time initialization. How To Change App.config File Runtime Using C# you could try out some sort of Profile WebService where you only specify one Web Service URL from the client and depending on Client's details (you might have Group/User level overrides), How To Update App.config File In C# What I was doing that I was debugging the application in visual studio.

Sign In·ViewThread·Permalink Re: Comment Problem UsualDosage17-Aug-06 9:41 UsualDosage17-Aug-06 9:41 In the load config method, you will see that the code loops through the XMLNodes. weblink Sign In·ViewThread·Permalink Another Approach jothar7331-Jul-07 9:40 jothar7331-Jul-07 9:40 I've been working on something similar that involves me programatically adding connection strings to a config file, but when using encryption on share|improve this answer edited Dec 20 '15 at 14:33 answered Jun 22 '12 at 2:49 Kevin Aenmey 9,45522638 How I can make the change then? –a1204773 Jun 22 '12 Sign In·ViewThread·Permalink Re: Correction: loading app.config at runtime if application is installed UsualDosage5-Oct-07 10:17 UsualDosage5-Oct-07 10:17 1) Your first point is correct in the event that you have an installed Update App.config Connection String At Runtime C#

Sign In·ViewThread·Permalink Thank you :) dariol16-Oct-07 23:05 dariol16-Oct-07 23:05 This code works and is easy to understand Thank you very much! Quae narravi nullo modo negabo. There are some insightful bits here: http://forums.microsoft.com/msdn/showpost.aspx?postid=112272&siteid=1[^]. http://cdhca.org/how-to/write-app-config-at-runtime.php The main method simply checks the value of name before and after changing the configuration.

However, the whole reason of this is to avoid shipping config files. C# Load App.config From Different Location Update failed."); XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings"); // Attempt to locate the requested setting. Alexandru Stanciu10-Jan-06 5:40 Alexandru Stanciu10-Jan-06 5:40 http://www.codeproject.com/csharp/ConfigurationSettingsRW.asp..

this worked like charm......thanks a ton.....Regards,sreeeSree Pida Sign In·ViewThread·Permalink My vote of 2 vnenad24-Feb-09 9:45 vnenad24-Feb-09 9:45 Author's comment about using hard-coded app.config isn't really mistake..

I don't understand, however, why there isn't a "set" function of some kind. Background Visual Studio .NET has hinted that more powerful support for modifying App.config files at runtime will come with the release of .NET 2.0. Unfortunately, it doesn't seem to do anything, because I still get the data from the normal app.config. C# Change App.config Value At Runtime You'll notice that when this article was written, it targeted the 1.1 Framework, but sure, that would be a good thing to use now.

What is the use of oil in this recipe? PS - WordPress is slow and horrible. It will utilize the helper method KeyExists to ensure we have a key to update.// Updates a key within the App.config public void UpdateKey(string strKey, string newValue) { if (!KeyExists(strKey)) throw his comment is here The negative order integer challenge, but it's Prime Time!

Dropbox Password security Do progress reports belong on stderr or stdout? We have been through this route where we had different static/dynamic config files depending on environment/profile/group and it became quite messy at the end. Any good ideas….. Why const_iterator could be used with std::map::erase Why is First past the post used in so many countries?

Linked 47 ConfigurationManager.AppSettings - How to modify and save? 18 Write values in app.config file 13 Change the value in app.config file dynamically 3 reading and writing app.config in C# 0 Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration("C:\anyapplication.exe");//config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("your web root"); ConnectionStringSettings cssNew = new ConnectionStringSettings(); cssNew.Name = title; cssNew.ProviderName = providerName; cssNew.ConnectionString = connString; config.ConnectionStrings.ConnectionStrings.Add(cssNew); config.Save(); Sign In·ViewThread·Permalink Re: Another Approach UsualDosage31-Jul-07