如何用VS2005制作Web安装程序

添加人:admin三级(1318分)   添加时间:2007-06-26    阅读次数:40555  收藏此教程
#region CreateWebsite 添加网站
public string CreateWebSite(string serverID, string serverComment, string defaultVrootPath, string HostName, string IP, string Port)
{
    try
    {
        ManagementObject oW3SVC = new ManagementObject(_scope, new ManagementPath(@"IIsWebService='W3SVC'"), null);

        if (IsWebSiteExists(serverID))
        {
            return "Site Already Exists...";
        }

        ManagementBaseObject inputParameters = oW3SVC.GetMethodParameters("CreateNewSite");
        ManagementBaseObject[] serverBinding = new ManagementBaseObject[1];
        serverBinding[0] = CreateServerBinding(HostName, IP, Port);
        inputParameters["ServerComment"] = serverComment;
        inputParameters["ServerBindings"] = serverBinding;
        inputParameters["PathOfRootVirtualDir"] = defaultVrootPath;
        inputParameters["ServerId"] = serverID;
        
        ManagementBaseObject outParameter = null;
        outParameter = oW3SVC.InvokeMethod("CreateNewSite", inputParameters, null);
        
        // 启动网站
        string serverName = "W3SVC/" + serverID;
        ManagementObject webSite = new ManagementObject(_scope, new ManagementPath(@"IIsWebServer='" + serverName + "'"), null);
        webSite.InvokeMethod("Start", null);

        return (string)outParameter.Properties["ReturnValue"].Value;
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
}

public ManagementObject CreateServerBinding(string HostName, string IP, string Port)
{
    try
    {
        ManagementClass classBinding = new ManagementClass(_scope, new ManagementPath("ServerBinding"), null);
        ManagementObject serverBinding = classBinding.CreateInstance();
        serverBinding.Properties["Hostname"].Value = HostName;
        serverBinding.Properties["IP"].Value = IP;
        serverBinding.Properties["Port"].Value = Port;
        serverBinding.Put();
        return serverBinding;
    }
    catch
    {
        return null;
    }
}
#endregion

好了,准备工作已经做完,现在开始写最重要的Install方法了
整个方法写完后如下:
#region Install 安装
///
/// 安装数据库
///
///
public override void Install(IDictionary stateSaver)
{
    base.Install(stateSaver);

    dir = this.Context.Parameters["dir"];

    DBName = this.Context.Parameters["DBNAME"].ToString();
    ServerName = this.Context.Parameters["server"].ToString();
    AdminName = this.Context.Parameters["user"].ToString();
    AdminPwd = this.Context.Parameters["pwd"].ToString();
    iis = this.Context.Parameters["iis"].ToString(); ;
    port = this.Context.Parameters["port"].ToString();
    
    //写入获取的安装程序中的变量,此段代码为调试用可以不添加
    this.sqlConn.ConnectionString = "Packet size=4096;User ID=" + AdminName + ";Data Source=" + ServerName + ";Password=" + AdminPwd + ";Persist Security Info=False;Integrated Security=false";

    // 执行SQL 安装数据库 可选择时恢复或者时直接创建
    if(!CreateDBAndTable(DBName))
    {
        throw new ApplicationException("创建数据库时出现严重错误!");
    }
    

    // 从备份数据库文件恢复数据库
    /*
    if (!RestoreDB(DBName))
    {
        throw new ApplicationException("恢复数据库时出现严重错误!");
    }
    */


    // 添加网站
    Connect();
    //string serverID = GetNextOpenID().ToString();
    //string serverComment = websitenName;

             // 下面的信息为测试,可以自己编写文本框来接收用户输入信息
    string serverID = "5555";
    string serverComment = "cqfeng";
    string defaultVrootPath = this.Context.Parameters["targetdir"];
    if (defaultVrootPath.EndsWith(@"\"))
    {
        defaultVrootPath = defaultVrootPath.Substring(0, defaultVrootPath.Length-1);
    }
    string HostName = "";
    string IP = "";
    string Port = port;
    string sReturn = CreateWebSite(serverID, serverComment, defaultVrootPath, HostName, IP, Port);
    
    // 修改web.config
    if (!WriteWebConfig())
    {
        throw new ApplicationException("设置数据库连接字符串时出现错误");
    }

    // 写注册表
    WriteRegistryKey();
}
#endregion

相关的教程: VS2005 部署 安装 网站
收藏此教程

当前平均分: -1.8(8 次打分)

-5-4-3-2-1012345
评论主题
您的大名
您的评论
验证码 点击换一个验证码
知识库搜索: