遥远的青苹果

代码人生

 

多个应用项目使用共享配置方案!实例

项目一port是入口,启动项目,有一个app配置文件,demeAPPTwo是个第二个项目,这里为了模拟启动项目。Port用到了引用了demeAPPTwo。具体从port打开demeAPPTwo的窗体,demeAPPTwo的 窗体上调用自己项目内的AppConfigHelp类。获得全局的配置文件,当然路径时重点。

 

上代码:

  ///   /// ConfigClass 的摘要说明。

    public class AppConfigHelp

    {

        public string strFileName;

 

        public string configName;

 

        public string configValue;

        public AppConfigHelp()

        {

            //   // TODO: 在此处添加构造函数逻辑   //

        }

 

        public string ReadConfig(string configKey)

        {

            configValue = "";

            configValue = ConfigurationSettings.AppSettings["" + configKey + ""];

            return configValue;

        }

        //得到程序的config文件的名称以及其所在的全路径

        public void SetConfigName(string strConfigName)

        {

            configName = strConfigName;   //获得配置文件的全路径

            GetFullPath();

        }

        public void GetFullPath()

        {   //获得配置文件的全路径

            strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + configName;

        }

        public void SaveConfig(string configKey, string configValue)

        {

            XmlDocument doc = new XmlDocument();

            doc.Load(strFileName);

            //找出名称为“add”的所有元素 

            XmlNodeList nodes = doc.GetElementsByTagName("add");

            for (int i = 0; i < nodes.Count; i++)

            {   //获得将当前元素的key属性 

                XmlAttribute att = nodes[i].Attributes["key"];

                //根据元素的第一个属性来判断当前的元素是不是目标元素 

                if (att.Value == "" + configKey + "")

                {   //对目标元素中的第二个属性赋值

                    att = nodes[i].Attributes["value"];

                    att.Value = configValue;

                    break;

                }

            }   //保存上面的修改 

 

            doc.Save(strFileName);

        }

 

 

        /// <summary>

        /// 读取Portal_.exe.config参数.

        /// </summary>

        /// <param name="name"></param>

        /// <returns></returns>

        public string GetDefinedEXEPathAppConfig(string key, string exe_config_path)

        {

            //System.Xml.XmlNode configNode = null;

          

            if (string.IsNullOrEmpty(exe_config_path))

            {

                string configFileName = Application.StartupPath + "\\Portal.exe.config";//请根据实际情况修改

 

                if (File.Exists(configFileName)==false)

                {

                    return "";

                }

              

               

                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

                doc.Load(configFileName);

                //string configString = @"[@add='" + name + "']";//请根据实际情况修改

                //configNode = doc.SelectSingleNode(configString);

 

 

                  XmlNodeList nodes = doc.GetElementsByTagName("add");

                  for (int i = 0; i < nodes.Count; i++)

                  {   //获得将当前元素的key属性

                      if (nodes[i].Attributes[0].Name == "key") //有些不是add key的形式的

                      {

                          XmlAttribute att = nodes[i].Attributes["key"];

                          //根据元素的第一个属性来判断当前的元素是不是目标元素 

                          if (att.Value == "" + key + "")

                          {   //对目标元素中的第二个属性赋值

                              att = nodes[i].Attributes["value"];

                              configValue = att.Value;

                              return configValue;

                              break;

                          }

                      }

                  }

            }

            else

            {

                string configFileName = Application.StartupPath + "\\" + exe_config_path;//请根据实际情况修改

                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

                doc.Load(configFileName);

                XmlNodeList nodes = doc.GetElementsByTagName("add");

                for (int i = 0; i < nodes.Count; i++)

                {   //获得将当前元素的key属性 

                    XmlAttribute att = nodes[i].Attributes["key"];

                    //根据元素的第一个属性来判断当前的元素是不是目标元素 

                    if (nodes[i].Attributes[0].Name == "key") //有些不是add key的形式的

                    {

                        if (att.Value == "" + key + "")

                        {   //对目标元素中的第二个属性赋值

                            att = nodes[i].Attributes["value"];

                            configValue = att.Value;

                            return configValue;

                            break;

                        }

                    }

                }

 

            }

            return "";

 

        }

    }

}

 

下面是是很简单了 调用 一看就懂  不写了

 

posted on 2011-12-20 22:09  遥远的青苹果-李院长  阅读(314)  评论(0编辑  收藏  举报

导航