web项目制作成安装包是为了方便发布到服务器上,本文主要讲了安装包制作,IIS部署,数据库安装,卸载时删除IIS网站和数据库博文参考链接:http://www.cnblogs.com/huxj/archive/2010/09/10/1823637.html 下面是本人通过网上 ...
web项目制作成安装包是为了方便发布到服务器上,本文主要讲了安装包制作,get='_blank'>IIS部署,数据库安装,卸载时删除IIS网站和数据库
博文参考链接:http://www.cnblogs.com/huxj/archive/2010/09/10/1823637.html
下面是本人通过网上资料和自己的思考总结进行补充,希望能对大家又所帮助。
首先我们需要一个已经发布的网站
一、安装包制作
打开网站
然后右键解决方案》添加》新建项目》其他项目类型》安装和部署》Visual Studio Installer 》安装项目
在web项目里新建一个安装项目,如图:
在新建的安装项目,右键安装项目》添加》项目输出,如图:
选择项目输出,选择要操作的项目,如图:
示例项目是一个web网站,所以只有一个内容文件输出选项,选中内容文件点击确定
如果示例项目是一个Web项目,则选择主输出,如下图:
现在我们来制作安装包的安装界面,如图选择用户界面:
右键启动,添加对话框,如图:
这里可以根据安装项目的需要来选择设置。
这里我们选择文本框(A)、文本框(B)、许可协议
拖动对话框进行排序——对话框的排序代表着安装时界面顺序。
然后我们在文件系统》应用程序文件夹,右边空白处右键》添加》文件,添加license.rtf文件 ,如图:
现在我们回到用户界面,选择用户协议对话框-属性
可以看到LicenseFile没有值,将刚添加进的license.rtf文件添加进去
选择应用程序文件夹
由于在安装过程也需要设置数据库,所以我们还需要让安装用户在安装过程中输入数据库服务器信息,选择对话框(A)-属性,设置如图:
这里我们只需要服务器、账号、密码,所以Edit4Visible设为false.
由于在安装过程中也需要设置IIS,所以还需要让安装用户在安装过程中输入网站配置,选择对话框(B)-属性,设置如图:
Value中的值都是为默认值
由于我们需要配置数据库和iis,所以我们需要在新建两个类库(upLibrary1和unLibrary1),各自添加一个安装程序类(添加的安装程序类是一个继承installer的类),如图:
其中upLibrary1类库是安装,unLibrary1类库是卸载。
现在我们在安装项目Setup添加这两个类库,右键》添加》项目输出》选择upLibrary1类库和unLibrary1类库》选择主输出
添加后
然后右键安装项目》自定义操作》右键安装》添加自定义操作》应用程序文件夹
选择主输出来自upLibrary1(活动)
卸载中选择主输出来自unLibrary1(活动)
接下来我们需要在安装过程传递输入的数据传递这个项目中,选择主输出来自upLibrary1(活动),右键属性,如图:
属性框中的CustomActionData就是指定要传递到安装程序的自定义数据。
/server="'[EDITA1]'" /user="'[EDITA2]'" /pwd="'[EDITA3]'" /iis="[IISSERVER]" /ip="[IP]" /port="[PORT]" /ISNAME="[ISNAME]" /targetdir="[TARGETDIR]\"
/targetdir="[TARGETDIR]\"中的“\”记得不要丢了
现在我们回到upLibrary1安装程序类:
我们先讲upLibrary1类库,先不管unLibrary1类库
打开upInstaller1类是一个设计视图,点击 单击此处切换到代码视图
在安装程序类里我们可以写安装过程中的处理事件,比如附加数据库,将网站发布到iis上。
首先我们先从写Install方法:
public override void Install(IDictionary stateSaver){ //这里面就是我们的主要代码区 }
在Install方法中我们可以接收安装过程中输出的数据信息,如下:
代码块:
安装:
里面包含数据库的附加、连接iis服务器、判断网站是否存在、添加网站
1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.ComponentModel; 5 using System.Configuration.Install; 6 using System.Linq; 7 using System.Data.SqlClient; 8 using System.Management; 9 using System.IO; 10 using System.Security.AccessControl; 11 using System.DirectoryServices; 12 13 14 namespace upLibrary1 15 { 16 [RunInstaller(true)] 17 public partial class upInstaller1 : System.Configuration.Install.Installer 18 { 19 public upInstaller1() 20 { 21 InitializeComponent(); 22 } 23 24 string iis = ""; 25 string port = ""; 26 private string _target; 27 private string ISNAME; 28 private string targetdir; //安装地址 29 private DirectoryEntry _iisServer; 30 private ManagementScope _scope; 31 private ConnectionOptions _connection; 32 33 public override void Install(IDictionary stateSaver) 34 { 35 36 base.Install(stateSaver); 37 string databaseServer = Context.Parameters["server"].ToString(); //数据库服务器 38 //string databasename = Context.Parameters["dbname"].ToString(); 39 string userName = Context.Parameters["user"].ToString(); //账号 40 string userPass = Context.Parameters["pwd"].ToString(); //密码 41 string targetdir = Context.Parameters["targetdir"].ToString(); //安装地址 42 iis = this.Context.Parameters["iis"].ToString(); //服务器 43 string ip = this.Context.Parameters["ip"].ToString(); //ip 44 port = this.Context.Parameters["port"].ToString(); //端口 45 ISNAME = this.Context.Parameters["ISNAME"].ToString(); //网站名 46 47 string serverID = "66"; //和iis上的网站ID不可重复 48 try 49 { 50 //System.Diagnostics.Debugger.Launch(); //调试代码 51 Connect(); 52 string serverComment = ISNAME; 53 string defaultVrootPath = this.Context.Parameters["targetdir"]; 54 if (defaultVrootPath.EndsWith(@"\")) 55 { 56 defaultVrootPath = defaultVrootPath.Substring(0, defaultVrootPath.Length - 1); 57 } 58 string HostName = ""; 59 string IP = ip; 60 string Port = port; 61 string sReturn = CreateWebSite(serverID, serverComment, defaultVrootPath, HostName, IP, Port); 62 63 //给文件添加"Authenticated Users,Everyone,Users"用户组的完全控制权限 64 if (File.Exists(Context.Parameters["targetdir"].ToString() + "App_Data\\jiaowuDB.mdf")) 65 { 66 FileInfo fi = new FileInfo(Context.Parameters["targetdir"].ToString() + "App_Data\\jiaowuDB.mdf"); 67 System.Security.AccessControl.FileSecurity fileSecurity = fi.GetAccessControl(); 68 fileSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow)); 69 fileSecurity.AddAccessRule(new FileSystemAccessRule("Authenticated Users", FileSystemRights.FullControl, AccessControlType.Allow)); 70 fileSecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow)); 71 fi.SetAccessControl(fileSecurity); 72 FileInfo fi1 = new FileInfo(Context.Parameters["targetdir"].ToString() + "App_Data\\jiaowuDB.ldf"); 73 System.Security.AccessControl.FileSecurity fileSecurity1 = fi1.GetAccessControl(); 74 fileSecurity1.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow)); 75 fileSecurity1.AddAccessRule(new FileSystemAccessRule("Authenticated Users", FileSystemRights.FullControl, AccessControlType.Allow)); 76 fileSecurity1.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow)); 77 fi1.SetAccessControl(fileSecurity1); 78 } 79 80 string connectionString = GetConnectionString(null); 81 //保存数据连接词,为卸载做准备 82 File.WriteAllText(Path.Combine(targetdir + "App_Data\\", "log.txt"), connectionString); 83 try 84 { 85 using (SqlConnection connection = new SqlConnection(connectionString)) 86 { 87 connection.Open(); 88 //使用数据库文件创建数据库,所以添加的网站项目中需要有App_Data文件夹和数据库文件(jiaowuDB.mdf)和日志文件(jiaowuDB.ldf) 89 string sql = "sp_attach_db 'jiaowuDB','" + Context.Parameters["targetdir"].ToString() + "App_Data\\jiaowuDB.mdf','" 90 + Context.Parameters["targetdir"].ToString() + "App_Data\\jiaowuDB.ldf'"; 91 ExecuteSQL(connection, sql); 92 connection.Close(); 93 //修改config文件连接词 94 string webconfigpath = Path.Combine(this.Context.Parameters["targetdir"].ToString(), "web.config"); 95 string webcofnigstring = File.ReadAllText(webconfigpath).Replace("#constring#", GetConnectionString("jiaowuDB")); 96 File.WriteAllText(webconfigpath, webcofnigstring); 97 98 } 99 }100 catch (Exception e)101 {102 throw new Exception(e.Message);103 }104 }105 catch (Exception ex)106 {107 base.Rollback(stateSaver);108 109 throw new Exception(ex.Message);110 }111 }112 113 #region Connect 连接IIS服务器114 public bool Connect()115 {116 117 if (iis == null)118 return false;119 try120 {121 _iisServer = new DirectoryEntry("IIS://" + iis + "/W3SVC/1");122 _target = iis;123 _connection = new ConnectionOptions();124 _scope = new ManagementScope(@"//" + iis + @"/root/MicrosoftIISV2", _connection);125 _scope.Connect();126 }127 catch128 {129 130 return false;131 }132 return IsConnected();133 }134 135 public bool IsConnected()136 {137 if (_target == null || _connection == null || _scope == null) return false;138 return _scope.IsConnected;139 }140 #endregion141 142 #region CreateWebsite 添加网站143 public string CreateWebSite(string serverID, string serverComment, string defaultVrootPath, string HostName, string IP, string Port)144 {145 try146 {147 ManagementObject oW3SVC = new ManagementObject(_scope, new ManagementPath(@"IIsWebService='W3SVC'"), null);148 149 if (IsWebSiteExists(serverID))150 {151 throw new Exception("服务器上已存在" + ISNAME);152 }153 154 ManagementBaseObject inputParameters = oW3SVC.GetMethodParameters("CreateNewSite");155 ManagementBaseObject[] serverBinding = new ManagementBaseObject[1];156 serverBinding[0] = CreateServerBinding(HostName, IP, Port);157 inputParameters["ServerComment"] = serverComment;158 inputParameters["ServerBindings"] = serverBinding;159 inputParameters["PathOfRootVirtualDir"] = defaultVrootPath;160 inputParameters["ServerId"] = serverID;161 162 ManagementBaseObject outParameter = null;163 outParameter = oW3SVC.InvokeMethod("CreateNewSite", inputParameters, null);164 165 // 启动网站166 //string serverName = "W3SVC/" + serverID;167 //ManagementObject webSite = new ManagementObject(_scope, new ManagementPath(@"IIsWebServer='" + serverName + "'"), null);168 //webSite.InvokeMethod("Start", new object[] {});169 170 return (string)outParameter.Properties["ReturnValue"].Value;171 }172 catch (Exception ex)173 {174 throw new Exception(ex.Message);175 }176 }177 178 public ManagementObject CreateServerBinding(string HostName, string IP, string Port)179 {180 try181 {182 ManagementClass classBinding = new ManagementClass(_scope, new ManagementPath("ServerBinding"), null);183 ManagementObject serverBinding = classBinding.CreateInstance();184 serverBinding.Properties["Hostname"].Value = HostName;185 serverBinding.Properties["IP"].Value = IP;186 serverBinding.Properties["Port"].Value = Port;187 serverBinding.Put();188 return serverBinding;189 }190 catch191 {192 return null;193 }194 }195 #endregion196 197 #region IsWebSiteExists 判断网站是否已经存在198 public bool IsWebSiteExists(string serverID)199 {200 try201 {202 string siteName = "W3SVC/" + serverID;203 ManagementObjectSearcher searcher = new ManagementObjectSearcher(_scope, new ObjectQuery("SELECT * FROM IIsWebServer"), null);204 205 ManagementObjectCollection webSites = searcher.Get();206 foreach (ManagementObject webSite in webSites)207 {208 if ((string)webSite.Properties["Name"].Value == siteName)209 return true;210 }211 212 return false;213 }214 catch215 {216 return false;217 }218 }219 #endregion220 221 /// <summary>222 /// 执行sql语句223 /// </summary>224 /// <param name="connection"></param>225 /// <param name="sql"></param>226 void ExecuteSQL(SqlConnection connection, string sql)227 {228 SqlCommand cmd = new SqlCommand(sql, connection);229 cmd.ExecuteNonQuery();230 }231 232 233 /// <summary>234 /// 获取数据库登录连接字符串235 /// </summary>236 /// <param name="databasename">数据库名称</param>237 /// <returns></returns>238 private string GetConnectionString(string databasename)239 {240 return "server=" + Context.Parameters["server"].ToString() + ";database=" + (string.IsNullOrEmpty(databasename) ? "master" : databasename) + ";User ID=" + Context.Parameters["user"].ToString() + ";Password=" + Context.Parameters["pwd"].ToString();241 }242 243 }244 }
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:C# Web项目制作安装包
关键词:C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。