標籤:des style blog http io ar os 使用 sp
原文 用Inno Setup製作WEB程式安裝包
最近做了一個WEB程式的安裝包,我把製作的過程做個介紹,貼出源碼給大家做個參考
看看inno 的指令碼
[Setup]AppCopyright=testAppName=testAppVerName=test v2.0SolidCompression=trueOutputDir=OutputOutputBaseFilename=test_setupDefaultDirName={pf}\LmsDefaultGroupName=Lms;安裝程式的基本資料[_ISTool]UseAbsolutePaths=false[UninstallDelete]Type: files; Name: {app}\init_test.logType: dirifempty; Name: {app}\database;需要提示卸載程式額外刪除的目錄[Run]Filename: {tmp}\SetACL.EXE; Parameters: "-ot file -on ""{app}"" -actn ace -ace ""n:S-1-5-20;s:y;p:change,full"""; Flags: runhidden waituntilterminated skipifdoesntexistFilename: {tmp}\SetACL.EXE; Parameters: "-ot file -on ""{app}"" -actn ace -ace ""n:S-1-5-32-545;s:y;p:change,full"""; Flags: runhidden waituntilterminated skipifdoesntexistFilename: {tmp}\SetACL.EXE; Parameters: "-ot file -on ""{app}"" -actn ace -ace ""n:S-1-1-0;s:y;p:change,full"""; Flags: runhidden waituntilterminated skipifdoesntexistFilename: {tmp}\SetACL.EXE; Parameters: "-ot file -on ""{app}\database"" -actn ace -ace ""n:S-1-1-0;s:y;p:change,full"""; Flags: runhidden waituntilterminated skipifdoesntexistFilename: {tmp}\init_test.exe; Parameters: """{app}"" 2.0"; Description: Configure SQLServer; StatusMsg: Configuring Database; Flags: postinstall skipifdoesntexist;uncomment this line to use for dotnet 1.1;Filename: {tmp}\SetACL.exe; Parameters: "-ot file -on ""{win}\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files"" -actn ace -ace ""n:S-1-1-0;s:y;p:change,full"""; Flags: runhiddenFilename: {tmp}\SetACL.exe; Parameters: "-ot file -on ""{win}\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files"" -actn ace -ace ""n:S-1-1-0;s:y;p:change,full"""; Flags: runhidden;安裝過程需要啟動並執行程式,SetACL.EXE是一個第三方組件使用方法大家可以google一下,init_test.exe為附加資料庫和資料還有設定asp.net版本的程式[Dirs]Name: {app}\databaseName: {app}\course;產生目錄[Types]Name: Custom; Description: Custom installation; Flags: iscustom[Files]Source: 3rdParty\SetACL.exe; DestDir: {tmp}; flags: deleteafterinstallSource: test\*; DestDir: {app}; Excludes:*.webinfo,*.vspscc, \obj,Thumbs.db,CVS,*.pdb,*.cs,*.scc,*.bak,*.csproj,*.log,*.Old,*.user,*.lic,*.sln,*.suo,8.rar; Flags: recursesubdirsSource: test_table.sql; DestDir: {tmp}; Flags: deleteafterinstallSource: test_Data.sql; DestDir: {tmp}; Flags: deleteafterinstallSource: init_test.exe; DestDir: {tmp}; flags: deleteafterinstall;需要隨安裝包一起打包的檔案[Code]const VDirName = ‘test‘; Vwebctrl = ‘webctrl_client‘; IISServerNumber = ‘1‘; function SafeCreateOleObject(ProgId:String;ExceptionMsg:String):Variant;var retobj:Variant;begin try retobj := CreateOleObject(ProgId); except RaiseException(ExceptionMsg+‘‘#13#13‘(Error ‘‘‘ + GetExceptionMessage + ‘‘‘ occurred)‘); end; Result:=retobj;end;{ create virtual directory, pointing to installation directory}procedure SetupIIS;var IIS, WebSite, WebServer, WebRoot, VDir: Variant;begin { Create the main IIS COM Automation object } IIS:=SafeCreateOleObject(‘IISNamespace‘,‘Please install Microsoft IIS first.‘); { Connect to the IIS server } WebSite := IIS.GetObject(‘IIsWebService‘, GetComputerNameString() + ‘/w3svc‘); WebServer := WebSite.GetObject(‘IIsWebServer‘, IISServerNumber); WebRoot := WebServer.GetObject(‘IIsWebVirtualDir‘, ‘Root‘); { (Re)create a virtual dir } try WebRoot.Delete(‘IIsWebVirtualDir‘, VDirName); except end; try WebRoot.Delete(‘IIsWebVirtualDir‘, Vwebctrl); except end; If DirExists(ExpandConstant(‘{app}‘)) then begin VDir := WebRoot.Create(‘IIsWebVirtualDir‘, VDirName); VDir.AccessRead := True; VDir.AccessFlags:=529; VDir.AppFriendlyName := ‘LMS Website‘; VDir.Path := ExpandConstant(‘{app}\‘); VDir.EnableDirBrowsing:=False; VDir.EnableDefaultDoc:=True; VDir.DefaultDoc :=‘Default.aspx‘; VDir.AppCreate(True); VDir.SetInfo(); end; If DirExists(ExpandConstant(‘{app}\IEWebControl\‘+Vwebctrl)) then begin VDir := WebRoot.Create(‘IIsWebVirtualDir‘, Vwebctrl); VDir.AccessRead := True; VDir.AccessFlags:=529; VDir.AppFriendlyName := ‘visual web ctral‘; VDir.Path := ExpandConstant(‘{app}\IEWebControl\‘+Vwebctrl); VDir.EnableDirBrowsing:=False; VDir.EnableDefaultDoc:=True; VDir.DefaultDoc :=‘default.htm‘; VDir.AppCreate(True); VDir.SetInfo(); end;end;procedure ControlIIS(bState:boolean);var resultcode:integer; param:string;begin if bState then param:=‘START‘ else param:=‘STOP‘; Exec(‘NET.EXE‘,param+‘ "IIS ADMIN"‘, ExpandConstant(‘{sys}‘),SW_SHOW,ewWaitUntilTerminated,resultcode );end;;在IIS預設網站下添加虛擬目錄procedure CurStepChanged(CurStep: TSetupStep);begin case CurStep of ssPostInstall: begin SetupIIS(); //ControlIIS(true); end; ssInstall: //ControlIIS(false); else ; end;end;
再來看一下init_lms.exe的主要方法:
private void CreateDatabase() { string sql = " CREATE DATABASE " + dbName; if (IsLocalInstall()) { sql += " ON PRIMARY (NAME = " + dbDataName + ", " + " FILENAME = ‘" + getDir("database\\" + dbDataName + ".mdf") + "‘, " + " SIZE = 5MB," + " FILEGROWTH =1) " + " LOG ON (NAME =" + dbLogName + ", " + " FILENAME = ‘" + getDir("database\\" + dbLogName + ".ldf") + "‘, " + " SIZE = 1MB, " + " FILEGROWTH =1) "; } sql += " COLLATE Chinese_PRC_CI_AS"; try { this.Cursor = Cursors.WaitCursor; frmProg.StepText("drop database"); if (chkCreateDB.Checked) { try { execSQL("master", "DROP DATABASE " + dbName); writeLog("old database dropped"); } catch (Exception) { writeLog("database not found or unable to be dropped"); } } frmProg.StepText("creating database"); if (chkCreateDB.Checked) { try { execSQL("master", sql); writeLog("DB CREATED:" + sql); } catch (Exception e) { writeLog("DB CREATING ERROR:" + e.Message); } } frmProg.StepText("creating tables"); if (chkTables.Checked) { writeLog("exec lmsdb_table.sql"); dmoExecSQL(sqlb_t.ToString());//dbName, sqlb); } frmProg.StepText("initialize base data"); if (chkDatas.Checked) { //writeLog(sqlb.ToString()); writeLog("exec lmsbasedata.sql"); dmoExecSQL(sqlb_d.ToString());//execSQL(dbName, sqlb); } frmProg.StepText("database initialization is done"); } finally { this.Cursor = Cursors.Default; } }
這個方法主要是建立資料庫檔案
private void setAspNetVersion(string m,string i) { string dotnetdir=addBackSlash(Environment.GetEnvironmentVariable("windir"))+"Microsoft.Net"; string[] dirs = Directory.GetDirectories(dotnetdir+"\\Framework\\"); foreach (string d in dirs) { int p = d.LastIndexOf("\\v"); if (p >= 0) { string v = d.Substring(p + 2); aspver = v; string[] mi = v.Split(new char[] { ‘.‘ }); if (mi[0].CompareTo(m)==0 && mi[1].CompareTo(i)>=0) { //found the directory string regiis = d + "\\aspnet_regiis.exe"; if (File.Exists(regiis)){ Process proc=new Process(); try{ proc.StartInfo.FileName=regiis; proc.StartInfo.Arguments = "-s W3SVC/1/ROOT/Lms"; proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; proc.StartInfo.CreateNoWindow=true; proc.Start(); proc.WaitForExit(10000); }catch (Win32Exception e){ if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND) { Console.WriteLine(e.Message + ". Check the path."); } else if (e.NativeErrorCode == ERROR_ACCESS_DENIED) { Console.WriteLine(e.Message + ". You do not have permission to run."); } } //catch }//if file.exists }//if compare version }//if pos>=0 }//for each }
這個方法主要是利用aspnet_regiis.exe來修改ASP.NET的版本,這個檔案在Microsoft.Net目錄下
用Inno Setup製作WEB程式安裝包