C#打包--如何用VS2005制作Web安装程序(下)
C#打包--如何用VS2005制作Web安装程序(下)
#region WriteRegistryKey 写注册表。安装部署中,直接有一个注册表编辑器,可以在那里面设置。 private void WriteRegistryKey() { // 写注册表 RegistryKey hklm = Registry.LocalMachine; RegistryKey cqfeng = hklm.OpenSubKey("SOFTWARE", true); RegistryKey F = cqfeng.CreateSubKey("cqfeng"); F.SetValue("FilePath", "kkkk"); } #endregion
操作IIS,建立网站等。可参考:
用VS2005制作网页对IIS进行操作
#region Connect 连接IIS服务器 public bool Connect() { if (iis == null) return false; try { _iisServer = new DirectoryEntry("IIS://" + iis + "/W3SVC/1"); _target = iis; _connection = new ConnectionOptions(); _scope = new ManagementScope(@"\" + iis + @"\root\MicrosoftIISV2", _connection); _scope.Connect(); } catch { return false; } return IsConnected(); } public bool IsConnected() { if (_target == null || _connection == null || _scope == null) return false; return _scope.IsConnected; } #endregion #region IsWebSiteExists 判断网站是否已经存在 public bool IsWebSiteExists(string serverID) { try { string siteName = "W3SVC/" + serverID; ManagementObjectSearcher searcher = new ManagementObjectSearcher(_scope, new ObjectQuery("SELECT * FROM IIsWebServer"), null); ManagementObjectCollection webSites = searcher.Get(); foreach (ManagementObject webSite in webSites) { if ((string)webSite.Properties["Name"].Value == siteName) return true; } return false; } catch { return false; } } #endregion #region GetNextOpenID 获得一个新的ServerID private int GetNextOpenID() { DirectoryEntry iisComputer = new DirectoryEntry("IIS://localhost/w3svc"); int nextID = 0; foreach (DirectoryEntry iisWebServer in iisComputer.Children) { string sname = iisWebServer.Name; try { int name = int.Parse(sname); if (name > nextID) { nextID = name; } } catch { } } return ++nextID; } #endregion #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("创建数据库时出现严重错误!"); } // 从备份数据库文件恢复数据库 // 添加网站 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
删除时的方法。在本文中未详细操作,比如删除站点,删除数据库等。如果需要,请你自己补足
#region Uninstall 删除 public override void Uninstall(IDictionary savedState) { if (savedState == null) { throw new ApplicationException("未能卸载!"); } else { base.Uninstall(savedState); } } #endregion
编译,然后选择安装,如图:
第一图:
第二图:
第三图:
抱歉,我不知道在这里怎么使登录密码框输入时显示为*号
第四图:
第五图:
安装:
安装完成:
安装后的IIS
安装目录:
安装后的数据库:
至此,一个简单的部署web程序的exe文件已经完成,当然,省略了很多东西,比如,对安装机器的判断(IIS版本,Framework版本,SQL
Server版本等),IIS站点属性等设置(默认页面,访问权限,执行权限等),卸载程序时应该删除的东西等等。这些东西,在MSDN里面可以查找到相关说明,如果你需要,只有辛苦一下了,嘿嘿。
相信有了这些,自己用WinForm来写安装程序也时可以了哈。

浙公网安备 33010602011771号