65.9K
CodeProject 正在变化。 阅读更多。
Home

安装期间修改 Web.Config

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.59/5 (18投票s)

2006年1月26日

2分钟阅读

viewsIcon

155390

downloadIcon

952

如何在安装过程中修改 WebService 的 Web.Config。

摘要

此应用程序演示了一个安装程序,它根据用户输入在设置期间更改 webservice 的 web.config 文件中的值。该应用程序更改 web.config 文件中关键值的数值。

解释

由于 web.config 是 ASP.NET 应用程序或 ASP.NET Web 服务应用程序的配置文件,用于存储数据,因此可以在应用程序设置后使用记事本或以编程方式对其进行修改。大多数程序员使用 web.config,因为它存储了用户可以动态更改的数据。

<appSettings>
  <add key="Name"  value="Keyvalue"/>
</appSettings>

如果 Web 应用程序或服务经常从一台服务器移动到另一台服务器,用户可能需要多次打开并更改此文件,因此我在安装程序中启用了一种功能,允许用户更改其选择的属性值。

我的示例在此应用程序中更改了 Web 服务使用的连接字符串。

<appSettings>
  <add key="Main.ConnectionString" 
        value="server=(local);database=DatabaseName;
               User ID=sa;Password=;
               trusted_connection=false"/>
</appSettings>

因此,在安装程序中,我通过在 webservice 项目中实现安装程序类来更改键值。

然后在安装程序类中,我重写了 Install 函数

public override void Install(IDictionary stateSaver)
{
    //You type here your own code
    CreateConnectionString();
    base.Install (stateSaver);
}

为了从用户那里获取更改 web.config 中值所需的数据,我在设置项目中添加了一个自定义操作

我将自定义数据属性更改为

/Server=[EDITA1]@/Username=
      [EDITA2]@/Password=[EDITA3]@/Folder=[EDITA4]

[EDITA1][EDITA2][EDITA3][EDITA4] 是在设置项目界面中添加的对话框中的文本框的名称。

您会注意到这里的数值

/Server=[EDITA1]@/Username=[EDITA2]@/Password=[EDITA3]@/Folder=[EDITA4]

我将参数与特殊字符 (@) 分隔开,以便稍后拆分它们,因为 Visual Studio 将所有参数连接到第一个参数并清空其他参数。

void FormateParamters()
{
    string strServer,strUser , strPassword ,strFolder;
    EventLog.WriteEntry("TNT", Context.Parameters["Server"]);

    strServer=Context.Parameters["Server"].Split('@')[0];
    strUser=Context.Parameters["Server"].Split('@')[1];
    strPassword=Context.Parameters["Server"].Split('@')[2];
    strFolder=Context.Parameters["Server"].Split('@')[3];

    Context.Parameters["Server"]=strServer;
    Context.Parameters["Username"]=strUser.Split('=')[1];
    EventLog.WriteEntry("user",Context.Parameters["Username"]);

    Context.Parameters["Password"]=strPassword.Split('=')[1];
    EventLog.WriteEntry("Password",Context.Parameters["Password"]);

    Context.Parameters["Folder"]=strFolder.Split('=')[1];
    EventLog.WriteEntry("folder",Context.Parameters["Folder"]);

    EventLog.WriteEntry("Server",Context.Parameters["Server"]);
}

前面的函数通过使用特殊字符 (@) 拆分参数来格式化参数,以便我们稍后可以将其用于连接字符串重构函数

void CreateConnectionString()
{
    FormateParamters();

    Assembly ass=Assembly.GetExecutingAssembly();
    EventLog.WriteEntry ("Web.Config", 
             ass.GetName().Name+".Web.config");

    Stream stmConfig=ass.GetManifestResourceStream(
                   ass.GetName().Name+".Web.config");

    if(!Directory.Exists(Context.Parameters["Folder"]))
        Directory.CreateDirectory(Context.Parameters["Folder"]);

    FileStream stmPhysical=new FileStream(
        Context.Parameters["Folder"]+@"\Web.config",
        FileMode.Create);
    StreamReader srConfig=new StreamReader(stmConfig);
    StreamWriter swConfig=new StreamWriter(stmPhysical);

    string strConfig=srConfig.ReadToEnd();
    stmConfig.Close();
    strConfig=strConfig.Replace("server=(local);database" + 
              "=DatabaseName;User ID=sa;Password=;" + 
              "trusted_connection=false",NewConnection());


    swConfig.Write(strConfig);
    swConfig.Close ();
    stmPhysical.Close();

}

上面的函数从物理路径复制 web.config 并开始用新值替换旧键值。

注意:本地文件夹参数必须与安装 webservice 的路径相同。在这种情况下,它是

C:\intpub\wwwroot\webtest1\
© . All rights reserved.