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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jaish MathewsPosted Mar 29, 2010, 1:53 AM
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");
gunjan aroraPosted Mar 24, 2014, 6:22 AM
Hirendra SisodiyaPosted Mar 29, 2010, 4:40 AM
you can use my code in window application ..and 'appSettings' is just a node name.
thanks
Jaish MathewsPosted Mar 29, 2010, 4:21 AM
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
Sam HobbsPosted Mar 29, 2010, 4:01 AM
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.
GustavoPosted Mar 29, 2010, 2:08 AM
Thanks, I got it to work.
GustavoPosted Mar 29, 2010, 1:17 AM
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.
GustavoPosted Mar 29, 2010, 1:01 AM
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
Hirendra SisodiyaPosted Mar 29, 2010, 12:43 AM
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
GustavoPosted Mar 29, 2010, 12:36 AM
Thanks. I already found the first two that you mentioned. The third one I never saw it and I like it better. Thanks you.
Lalit MPosted Mar 29, 2010, 12:32 AM
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