zoukankan      html  css  js  c++  java
  • 写入配置文件以及验证网络是否连通

            /// <summary>
            /// 向App。config写入字符串
            /// </summary>
            /// <param name="AppKey"></param>
            /// <param name="AppValue"></param>
            public static void SetValue(string ConnenctionString, string strKey)
            {
                XmlDocument doc = new XmlDocument();
                //获得配置文件的全路径
                string strFileName = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

                //string strFileName = System.Windows.Forms.Application.StartupPath + "\App.config";
                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 == strKey)
                    {
                        //对目标元素中的第二个属性赋值
                        att = nodes[i].Attributes["value"];
                        att.Value = ConnenctionString;
                        break;
                    }
                }
                //保存上面的修改
                doc.Save(strFileName);
            }

           /// <summary>
            /// 读出配置文件,验证网络是否连通
            /// </summary>
            public bool TestMethods()
            {
                conSql = new SqlConnection(CPDJConfig.ConfigurationManager.AppSettings["CONNSQLSTRYW"]);
                try
                {
                    conSql.Open();
                    return true;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                }
                finally
                {
                    conSql.Close();
                }
                return false;
            }

  • 相关阅读:
    LeetCode 226. Invert Binary Tree
    LeetCode 221. Maximal Square
    LeetCode 217. Contains Duplicate
    LeetCode 206. Reverse Linked List
    LeetCode 213. House Robber II
    LeetCode 198. House Robber
    LeetCode 188. Best Time to Buy and Sell Stock IV (stock problem)
    LeetCode 171. Excel Sheet Column Number
    LeetCode 169. Majority Element
    运维工程师常见面试题
  • 原文地址:https://www.cnblogs.com/conghua/p/3157427.html
Copyright © 2011-2022 走看看