正文

app.config修改操作2006-05-11 15:34:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/Csharpsky/13897.html

分享到:

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

阅读(5354) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册