Hello:
In my project there is an item that is named 'app.config'. Is there a way I can programmatically change it?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jaish MathewsRank #225 · 11K reputationPosted Mar 28, 2010, 9:53 PM EDT
Windows application converts all App.Config to App.Config.exe. So you need to work with that file. Builtin finctionality available like below. I am livel using for one of my application. Let me aknow if any problems. You need to add a Ref to "System.Configuration" and add namespace "System.Configuration;"
NB: No need to load .config files manually. Application will automatically recognize it by using below classes.
Configfile part
Method for Runtime Modification
public static void UpdateToolTypeConfiguration(string toolType)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//Assigning new value to ToolType section
config.AppSettings.Settings["ToolType"].Value = toolType;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
Calling method
UpdateToolTypeConfiguration("AnalysisTool");
Lalit MRank #1,186 · 1.5K reputationPosted Mar 28, 2010, 8:32 PM EDT
http://chiragrdarji.wordpress.com/2008/09/25/how-to-change-appconfig-file-run-time-using-c/
http://generally.wordpress.com/2007/09/27/using-appconfig-for-user-defined-runtime-parameters/
Read/Write App.config
GustavoRank #1,990 · 732 reputationPosted Mar 28, 2010, 8:36 PM EDT
Thanks. I already found the first two that you mentioned. The third one I never saw it and I like it better. Thanks you.
Hirendra SisodiyaRank #258 · 9.8K reputationPosted Mar 28, 2010, 8:43 PM EDT
Hello Giorgio
App.config file is a xml file, you can write it on run time for example if you can
change any key value of app.config you can try this function:
public static void WriteSetting(string key, string value)
{
// load config document for current assembly
XmlDocument doc = doc.Load(Assembly.GetExecutingAssembly().Location + ".config");
// retrieve appSettings node
XmlNode node = doc.SelectSingleNode("//appSettings");
if (node == null)
{
throw new InvalidOperationException("appSettings section not found in config file.");
}
try
{
// select the 'add' element that contains the key
XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
if (elem != null)
{
// add value for key
elem.SetAttribute("value", value);
}
else
{
// key was not found so create the 'add' element
// and set it's key/value attributes
elem = doc.CreateElement("add");
elem.SetAttribute("key", key);
elem.SetAttribute("value", value);
node.AppendChild(elem);
}
doc.Save(Assembly.GetExecutingAssembly().Location + ".config");
}
catch
{
throw;
}
}
thanks
Please mark as answere if it helps
GustavoRank #1,990 · 732 reputationPosted Mar 28, 2010, 9:01 PM EDT
Thanks. I am playing around with one of the links that Lalit mentioned. I am not getting it to even thou it looked simple. I will try it fora bit more. If I dont get it to work, I will then look at yours... thanks
GustavoRank #1,990 · 732 reputationPosted Mar 28, 2010, 9:17 PM EDT
I tried yours, its a lot simpler then the others.
BUT... the line with doc.Load is stopping the program in being compiled.
XmlDocument doc = doc.Load(Assembly.GetExecutingAssembly().Location + ".config");
BTW: Is this for a web app or a C# windows? reason I ask is that I saw this line: XmlNode node = doc.SelectSingleNode("//appSettings");
Doesnt the "appSettings" mean its for a web app? I am a newbe so I am probably wrong. I need it for a Windows Application.
GustavoRank #1,990 · 732 reputationPosted Mar 28, 2010, 10:08 PM EDT
Thanks, I got it to work.
Sam HobbsRank #107 · 20.5K reputationPosted Mar 29, 2010, 12:01 AM EDT
Note that in a typical Forms application, there is a Propeties node with a Settings.Settings file. Open that; you can create settings there, which is the app.config file. In the settings, there are two types; Appliation and User. The Application settings are for use by the application but not normally modified by the user.
Jaish MathewsRank #225 · 11K reputationPosted Mar 29, 2010, 12:21 AM EDT
Want to say some thing about Settings. This is normallu using to keep static strings, may be a web service path. But string values which may change depends on environment needs to keep in Config file. Because
After config file modification, no application rebuild needed. You can use application as like
But any change in .Settings need another build to make the changes embedded to the library. .Setting values anre keeping embedded to the library
Hirendra SisodiyaRank #258 · 9.8K reputationPosted Mar 29, 2010, 12:40 AM EDT
you can use my code in window application ..and 'appSettings' is just a node name.
thanks
gunjan aroraRank #8,160 · 227 reputationPosted Mar 24, 2014, 2:22 AM EDT