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

 

 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";
        }
    }

你可能感兴趣的:(wpf,string,exception,null,assembly,class)