// use reflection to find the location of the config file
System.Reflecti on.Assembly asm =
System.Reflecti on.Assembly.Get ExecutingAssemb ly ();
System.IO.FileI nfo fi = new System.IO.FileI nfo
(String.Format ("{0}.config ", asm.Location));
if (!fi.Exists)
throw new Exception ("Missing config file");
// load the config file into the XML DOM.
System.Xml.XmlD ocument xmlConfig = new System.Xml.XmlD ocument
();
xmlConfig.Prese rveWhitespace = true;
xmlConfig.Load (fi.FullName);
// find the right node and change it to the new value
foreach (System.Xml.Xml Node node in
xmlConfig["configurat ion"]["appSetting s"])
{
if ((node.Name == "add") &&
(node.Attribute s.GetNamedItem ("key").Valu e ==
"Height"))
{
node.Attributes .GetNamedItem ("value").Va lue =
newHeight;
}
< similar blocks for other parameters >
}
// write out the new config file
xmlConfig.Save (fi.FullName);
Eliyahu
"MK" <michael.kieser @encorus.com> wrote in message
news:003101c351 be$39e57140$7d0 2280a@phx.gbl.. .[color=blue]
> Is there any way to write into the app config at runtime?
>
> I want to store position, height and width of my forms.[/color]
"Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
news:egyvPpcUDH A.1004@TK2MSFTN GP12.phx.gbl...[color=blue]
> This code should do:
>
> // use reflection to find the location of the config file
> System.Reflecti on.Assembly asm =
> System.Reflecti on.Assembly.Get ExecutingAssemb ly ();
> System.IO.FileI nfo fi = new System.IO.FileI nfo
> (String.Format ("{0}.config ", asm.Location));
> if (!fi.Exists)
> throw new Exception ("Missing config file");
>
> // load the config file into the XML DOM.
> System.Xml.XmlD ocument xmlConfig = new System.Xml.XmlD ocument
> ();
> xmlConfig.Prese rveWhitespace = true;
> xmlConfig.Load (fi.FullName);
>
> // find the right node and change it to the new value
> foreach (System.Xml.Xml Node node in
> xmlConfig["configurat ion"]["appSetting s"])
> {
> if ((node.Name == "add") &&
> (node.Attribute s.GetNamedItem ("key").Valu e ==
> "Height"))
> {
> node.Attributes .GetNamedItem ("value").Va lue =
> newHeight;
> }
> < similar blocks for other parameters >
> }
>
> // write out the new config file
> xmlConfig.Save (fi.FullName);
>
> Eliyahu
>
> "MK" <michael.kieser @encorus.com> wrote in message
> news:003101c351 be$39e57140$7d0 2280a@phx.gbl.. .[color=green]
> > Is there any way to write into the app config at runtime?
> >
> > I want to store position, height and width of my forms.[/color]
>
>[/color]
Comment