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

  • 相关阅读:
    Mobile GMaps - Google Map移动版
    365Key今天不能用了,感觉不爽
    推荐:对个人免费的杀毒软件[avast!]
    向 Visual Studio 2005 Tools for Office 迁移时,将 VBA 代码转换为 Visual Basic .NET
    令人向往的3000年生活(转载)
    也说技术人员创业
    痛恨3721的朋友们,装个avast! Antivirus吧
    很Cool很全的Google Map的应用
    关于.net的企业应用开发
    天天网摘(20050704)
  • 原文地址:https://www.cnblogs.com/conghua/p/3157427.html
Copyright © 2011-2022 走看看