wpf app.config 客户端进行配置和修改.

来源:互联网 发布:大数据为什么在贵阳 编辑:程序博客网 时间:2024/04/30 19:29
 

 public class ConfigSettings
    {
        private static string NodePath = "//system.serviceModel//client//endpoint";
        private ConfigSettings() { }
        public static string GetEndpointAddress()
        {
            return ConfigSettings.loadConfigDocument().SelectSingleNode(NodePath).Attributes["address"].Value;
        }
        public static string GetAddressForMap()
        {
            XmlDocument doc = loadConfigDocument();
            XmlNode appSettingsNode = doc.SelectSingleNode("configuration/appSettings");

            string value="";
            foreach (XmlNode childNode in appSettingsNode)
            {
                if (childNode.Attributes==null)continue ;
                if (childNode.Attributes["key"] !=null && childNode.Attributes["key"].Value == "mapurl")
                {

                    value= childNode.Attributes["value"].Value;
                    break;
                }
            }
            return value;
        }

        public bool KeyExists(string strKey)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "..\\..\\App.config");

            XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings");

            foreach (XmlNode childNode in appSettingsNode)
            {
                if (childNode.Attributes["key"].Value == strKey)
                    return true;
            }
            return false;
        }


        public static void SaveEndpointAddress(string endpointAddress, string mapurl)
        {             // load config document for current assembly        
            XmlDocument doc = loadConfigDocument();
            // retrieve appSettings node          
            XmlNode node = doc.SelectSingleNode(NodePath);
            if (node == null)
                throw new InvalidOperationException("Error. Could not find endpoint node in config file.");
            try
            {                 // select the 'add' element that contains the key             
                //XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));   
                node.Attributes["address"].Value = endpointAddress;


                XmlNode appSettingsNode = doc.SelectSingleNode("configuration/appSettings");

                foreach (XmlNode childNode in appSettingsNode)
                {
                    if (childNode.Attributes == null) continue;
                    if (childNode.Attributes["key"] != null &&  childNode.Attributes["key"].Value == "mapurl")
                    {
                        childNode.Attributes["value"].Value = mapurl;
                    }
                }

                doc.Save(getConfigFilePath());

            }

 


            catch (Exception e)
            { throw e; }
        }
        public static XmlDocument loadConfigDocument()
        {
            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 Assembly.GetExecutingAssembly().Location + ".config";
        }
    }