Re: Reading config file from another app

Steve Welborn <[email protected]> Tue, 14 Nov 2006 15:01:15 -0800
Newsgroups gmane.comp.windows.devel.dotnet.cx
Message-ID <[email protected]>
Excellent thank you for the time on this.

-----Original Message-----
From: Discussion relating to the specifics of the C#
and Managed C++
languages [mailto:[email protected]] On
Behalf Of Daniel Weese
Sent: Tuesday, November 14, 2006 5:59 PM
To: [email protected]
Subject: Re: [DOTNET-CX] Reading config file from
another app

It's a hybrid of some code I found online, and my own
code.

http://www.codeproject.com/csharp/xmlconfig.asp is the
project that helped
me.

Code is below. I'll post the Write portion when I get
it all working.

Dan Weese

private void formManageSettings_Load(object sender,
System.EventArgs e)
{
   string sValue = ""; //Primarily so I can see the
results of my string
   XmlDocument doc = loadConfigDocument();

   int iEnd = this.tabControlSettings.TabPages.Count;

   //Loop through all of the fields on the form
   //Since the form only has text and combo boxes, no
need for others just
yet.
   //Each field has the Config Setting Name in the Tag
property
   //so I can limit the lines of code. This way you
can loop through the
controls
   //instead of making a hard coded line for each
element.
   //As new fields are added to the form, no
additional code is required
   for (int i = 0; i < iEnd; i++)
   {
      foreach (Control ctrl in
this.tabControlSettings.TabPages[i].Controls)
      {
         if (ctrl is TextBox)
         {
            sValue = "//appSettings//add[@key='" +
ctrl.Tag.ToString() +
"']";
            ctrl.Text = GetValue(doc,sValue);
         }
         if (ctrl is ComboBox)
         {
            sValue = "//appSettings//add[@key='" +
ctrl.Tag.ToString() +
"']";
            ctrl.Text = GetValue(doc,sValue);
         }
      }
   }
}

private XmlNode node;

public string GetValue (XmlDocument doc, string key)
{
   return Convert.ToString(GetValue(doc,key,
typeof(string)));
}

public new object GetValue (XmlDocument doc, string
key, System.Type sType)
{
   object ro = String.Empty;
   string sNode = key.Substring(0,
key.LastIndexOf("//"));
   // retrieve the selected node
   try
   {
      node =  doc.SelectSingleNode(sNode);
      if( node != null )
      {
         // Xpath selects element that contains the
key
         XmlElement targetElem=
(XmlElement)node.SelectSingleNode(key) ;
         if (targetElem!=null)
         {
            ro = targetElem.GetAttribute("value");
         }
      }
      if (sType == typeof(string))
         return Convert.ToString(ro);
      else
         if (sType == typeof(bool))
      {
         if (ro.Equals("True") || ro.Equals("False"))
            return Convert.ToBoolean(ro);
         else
            return false;
      }
      else
         if (sType == typeof(int))
         return Convert.ToInt32(ro);
      else
         if (sType == typeof(double))
         return Convert.ToDouble(ro);
      else
         if (sType == typeof(DateTime))
         return Convert.ToDateTime(ro);
      else
         return Convert.ToString(ro);
   }
   catch
   {
      return String.Empty;
   }
}

private static XmlDocument loadConfigDocument()
{
   //Load the document once since we have hundreds of
properties to get
   XmlDocument doc = null;
   try
   {
      doc = new XmlDocument();
      doc.Load(getConfigFilePath());
      return doc;
   }
   catch (System.IO.FileNotFoundException e)
   {
      throw new Exception("No configuration file
found.", e);
   }
}

private static string getConfigFilePath()
{
   return Application.StartupPath +
"\\ReLeaf7.exe.config";
}

===================================
This list is hosted by DevelopMentorR
http://www.develop.com

View archives and manage your subscription(s) at
http://discuss.develop.com




____________________________________________________________________________________
Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail beta.
http://new.mail.yahoo.com

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com