动态修改web.config程序

           代码如下所示,这是一段来自ASP.NET网站部署的程序, DataBaseName、ServerName、InStallAdmin、InstallPassword、dir ,这几个参数来自安装程序界面输入(具体怎样部署,请百度),这样就可以动态地改变网站连接配置文件的连接语句。。。
    1. /*先看一下我的web.config文件,要针对具体的情况具体地分析!!!为了简化说明,一些没必要的内容省略了。*/
    2. <?xml version="1.0"?>
    3. <configuration>
    4. ……
    5.   
    6.   <studynet>
    7.     <connstr>user id=sa;pwd=;data source=(local);initial catalog=study;timeout=90</connstr>
    8.     
    9.   </studynet>
    10. ……
    11.   <connectionStrings>
    12.     <add name="ConnectionString" connectionString="Data Source=(local);Initial Catalog=study;User ID=sa;pwd=" providerName="System.Data.SqlClient"/>
    13.    
    14.   </connectionStrings>
    15. ……
    16. </configuration>
       
  1. /*通过观察以上代码发现,有两处连接内容需要修改的,分别位于configuration—studynet与configuration—connectionStrings,具体看代码,聪明的你一定会明白,在代码面前没有秘密!!!*/
  2.  private bool WriteWebConfig()
  3.         {
  4.             string DBName = this.Context.Parameters["DataBaseName"];
  5.             string DBServer = this.Context.Parameters["ServerName"];
  6.             string DBUser = this.Context.Parameters["InStallAdmin"];
  7.             string DBKey = this.Context.Parameters["InstallPassword"];
  8.             System.IO.FileInfo FileInfo = new System.IO.FileInfo(this.Context.Parameters["dir"] + "/web.config");
  9.             if (!FileInfo.Exists)
  10.             {
  11.                 throw new InstallException("Missing config file :" + this.Context.Parameters["dir"] + "/web.config");
  12.             }
  13.             System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
  14.             xmlDocument.Load(FileInfo.FullName);
  15.             bool FoundIt = false;
  16.             //对第一个连接修改
  17.             foreach (System.Xml.XmlNode Node in xmlDocument["configuration"]["studynet"])
  18.             {
  19.                 if (Node.Name == "connstr")
  20.                 {
  21.                     Node.InnerText = String.Format("Persist Security Info=False;Data Source={0};Initial Catalog={1};User ID={2};pwd={3}", DBServer, DBName, DBUser, DBKey);
  22.                     
  23.                 }
  24.             }
  25.             //对第二个连接修改
  26.             foreach (System.Xml.XmlNode Node in xmlDocument["configuration"]["connectionStrings"])
  27.             {
  28.                 if (Node.Name == "add")
  29.                 {
  30.                     if (Node.Attributes.GetNamedItem("name").Value == "ConnectionString")
  31.                     {
  32.                         Node.Attributes.GetNamedItem("connectionString").Value = String.Format("Persist Security Info=False;Data Source={0};Initial Catalog={1};User ID={2};pwd={3}", DBServer, DBName, DBUser, DBKey);
  33.                         FoundIt = true;
  34.                     }
  35.                 }
  36.             }
  37.             if (!FoundIt)
  38.             {
  39.                 throw new InstallException("Error when writing the config file: web.config");
  40.             }
  41.             xmlDocument.Save(FileInfo.FullName);
  42.             return FoundIt;
  43.         }
  44.         
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值