writing config-file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MK

    #1

    writing config-file

    Is there any way to write into the app config at runtime?

    I want to store position, height and width of my forms.
  • Eliyahu Goldin

    #2
    Re: writing config-file

    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=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]


    Comment

    • Dennis Redfield

      #3
      Re: writing config-file

      Eliyahu,

      Thanks for the code sample. I am pretty new at this but why did you use:

      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));

      rather than:


      AppDomain.Curre ntDomain.GetDat a("APP_CONFIG_F ILE")

      thanks,

      dlr

      "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

      Working...