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