A4LGSharedFunctions.ConfigUtil.GetConfigValue C# (CSharp) Method

GetConfigValue() public static method

public static GetConfigValue ( string keyname, int defaultValue ) : int
keyname string
defaultValue int
return int
        public static int GetConfigValue(string keyname, int defaultValue)
        {
            XmlDocument oXml = null;
            XmlNodeList oList = null;
            try
            {
                string pConfigFiles = GetConfigFile();
                string keyvalue = "";

                if (File.Exists(pConfigFiles))
                {
                    //NameValueCollection AppSettings = new NameValueCollection();
                    oXml = new XmlDocument();

                    oXml.Load(pConfigFiles);

                    oList = oXml.GetElementsByTagName("appSettings");
                    if (oList == null) return defaultValue;

                    //AppSettings = new NameValueCollection();
                    foreach (XmlNode oNode in oList)
                    {
                        foreach (XmlNode oKey in oNode.ChildNodes)
                        {
                            if ((oKey != null) && (oKey.Attributes != null))
                            {
                                if (oKey.Attributes["key"].Value.Equals(keyname))
                                {
                                    if (oKey.Attributes["value"].Value.Trim().Length > 0)
                                    {
                                        keyvalue = oKey.Attributes["value"].Value;

                                        //Try to convert to integer32
                                        if (Globals.IsInteger(keyvalue))
                                        {
                                            return (Convert.ToInt32(keyvalue));
                                        }
                                    }
                                }
                            }
                        }
                    }
                    oXml = null;

                }

                return defaultValue;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message + "\nTypically an error here is from an improperly formatted config file. \nThe structure(XML) is compromised by a change you made.");
                return defaultValue;
            }
            finally
            {
                oXml = null;
                oList = null;
            }
        }

Same methods

ConfigUtil::GetConfigValue ( string keyname, bool defaultValue ) : bool
ConfigUtil::GetConfigValue ( string keyname, double defaultValue ) : double
ConfigUtil::GetConfigValue ( string keyname ) : string
ConfigUtil::GetConfigValue ( string keyname, string defaultValue ) : string