using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;
using System.IO;
namespace SMRouterSender
{
public class ClassConfigurationSettings
{
/// <summary>
/// 修改配置文件(数据库连接字符串)
/// </summary>
/// <param name="connString"></param>
public static void UpdateConfig( string SMSenderDBIP, string SMSenderSaUserName, string SMSenderSaPassword)
{
try
{
string m_strFullPath = "";
Assembly Asm = Assembly.GetExecutingAssembly();
XmlDocument xmlDoc = new XmlDocument();
//正式用例
//m_strFullPath = Asm.Location.Substring(0, (Asm.Location.LastIndexOf("\\") + 1)) + "SMRouterSender.exe.config";
//测试用例
m_strFullPath = Asm.Location.Substring(0, (Asm.Location.LastIndexOf("bin"))) + "app.config";
xmlDoc.Load(m_strFullPath);
//文件属性的控制(读->写)
if ((File.GetAttributes(m_strFullPath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
File.SetAttributes(m_strFullPath, FileAttributes.Normal);
}
XmlNodeList nodeList = xmlDoc.SelectSingleNode("/configuration/connectionStrings").ChildNodes;
foreach (XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe = (XmlElement)xn;
if (GetIndex(xe) != -1)
{
string LastestValue = "workstation id=ACER;packet size=4096;user id=" + SMSenderSaUserName + ";data source=" + SMSenderDBIP + ";persist security info=True;initial catalog=SMSRouter;password=" + SMSenderSaPassword + ";";
xe.SetAttribute("connectionString", LastestValue);
}
}
xmlDoc.Save(m_strFullPath);
////文件属性的控制(写->读)
File.SetAttributes(m_strFullPath, FileAttributes.ReadOnly);
}
catch (System.NullReferenceException NullEx)
{
throw NullEx;
}
catch (Exception ex)
{
throw ex;
}
}
private static int GetIndex(XmlElement xe)
{
return xe.GetAttribute("name").IndexOf("SMSRouter");
}
}
}
评论