用Inno Setup制作WEB程序安装包
>
最近做了一个WEB程序的安装包,我把制作的过程做个介绍,贴出源码给大家做个参考
看看inno 的脚本
1[Setup]
2AppCopyright=test
3AppName=test
4AppVerName=test v2.0
5SolidCompression=true
6OutputDir=Output\
7OutputBaseFilename=test_setup
8DefaultDirName={pf}\Lms
9DefaultGroupName=Lms
10;安装程序的基本信息
11[_ISTool]
12UseAbsolutePaths=false
13
14[UninstallDelete]
15Type: files; Name: {app}\init_test.log
16Type: dirifempty; Name: {app}\database
17;需要提示卸载程序额外删除的目录
18[Run]
19Filename: {tmp}\SetACL.EXE; Parameters: "-ot file -on ""{app}"" -actn ace -ace ""n:S-1-5-20;s:y;p:change,full"""; Flags: runhidden waituntilterminated skipifdoesntexist
20Filename: {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 skipifdoesntexist
21Filename: {tmp}\SetACL.EXE; Parameters: "-ot file -on ""{app}"" -actn ace -ace ""n:S-1-1-0;s:y;p:change,full"""; Flags: runhidden waituntilterminated skipifdoesntexist
22Filename: {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 skipifdoesntexist
23Filename: {tmp}\init_test.exe; Parameters: """{app}"" 2.0"; Description: Configure SQLServer; StatusMsg: Configuring Database; Flags: postinstall skipifdoesntexist
24;uncomment this line to use for dotnet 1.1
25;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: runhidden
26Filename: {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
27;安装过程需要运行的程序,SetACL.EXE是一个第三方组件使用方法大家可以google一下,init_test.exe为附加数据库和数据还有设置asp.net版本的程序
28
29[Dirs]
30Name: {app}\database
31Name: {app}\course
32;产生目录
33[Types]
34Name: Custom; Description: Custom installation; Flags: iscustom
35
36[Files]
37Source: 3rdParty\SetACL.exe; DestDir: {tmp}; flags: deleteafterinstall
38Source: test\*; DestDir: {app}; Excludes:*.webinfo,*.vspscc, \obj,Thumbs.db,CVS,*.pdb,*.cs,*.scc,*.bak,*.csproj,*.log,*.Old,*.user,*.lic,*.sln,*.suo,8.rar; Flags: recursesubdirs
39Source: test_table.sql; DestDir: {tmp}; Flags: deleteafterinstall
40Source: test_Data.sql; DestDir: {tmp}; Flags: deleteafterinstall
41Source: init_test.exe; DestDir: {tmp}; flags: deleteafterinstall
42;需要随安装包一起打包的文件
43[Code]
44const
45 VDirName = 'test';
46 Vwebctrl = 'webctrl_client';
47 IISServerNumber = '1';
48
49function SafeCreateOleObject(ProgId:String;ExceptionMsg:String):Variant;
50var
51 retobj:Variant;
52begin
53 try
54 retobj := CreateOleObject(ProgId);
55 except
56 RaiseException(ExceptionMsg+''#13#13'(Error ''' + GetExceptionMessage + ''' occurred)');
57 end;
58 Result:=retobj;
59end;
60
61{
62 create virtual directory, pointing to installation directory
63}
64
65procedure SetupIIS;
66var
67 IIS, WebSite, WebServer, WebRoot, VDir: Variant;
68begin
69 { Create the main IIS COM Automation object }
70 IIS:=SafeCreateOleObject('IISNamespace','Please install Microsoft IIS first.');
71 { Connect to the IIS server }
72
73 WebSite := IIS.GetObject('IIsWebService', GetComputerNameString() + '/w3svc');
74 WebServer := WebSite.GetObject('IIsWebServer', IISServerNumber);
75 WebRoot := WebServer.GetObject('IIsWebVirtualDir', 'Root');
76
77 { (Re)create a virtual dir }
78
79 try
80 WebRoot.Delete('IIsWebVirtualDir', VDirName);
81 except
82 end;
83 try
84 WebRoot.Delete('IIsWebVirtualDir', Vwebctrl);
85 except
86 end;
87
88 If DirExists(ExpandConstant('{app}')) then
89 begin
90 VDir := WebRoot.Create('IIsWebVirtualDir', VDirName);
91 VDir.AccessRead := True;
92 VDir.AccessFlags:=529;
93 VDir.AppFriendlyName := 'LMS Website';
94 VDir.Path := ExpandConstant('{app}\');
95 VDir.EnableDirBrowsing:=False;
96 VDir.EnableDefaultDoc:=True;
97 VDir.DefaultDoc :='Default.aspx';
98 VDir.AppCreate(True);
99 VDir.SetInfo();
100 end;
101
102 If DirExists(ExpandConstant('{app}\IEWebControl\'+Vwebctrl)) then
103 begin
104 VDir := WebRoot.Create('IIsWebVirtualDir', Vwebctrl);
105 VDir.AccessRead := True;
106 VDir.AccessFlags:=529;
107 VDir.AppFriendlyName := 'visual web ctral';
108 VDir.Path := ExpandConstant('{app}\IEWebControl\'+Vwebctrl);
109 VDir.EnableDirBrowsing:=False;
110 VDir.EnableDefaultDoc:=True;
111 VDir.DefaultDoc :='default.htm';
112 VDir.AppCreate(True);
113 VDir.SetInfo();
114 end;
115end;
116procedure ControlIIS(bState:boolean);
117var
118 resultcode:integer;
119 param:string;
120begin
121 if bState then
122 param:='START'
123 else param:='STOP';
124
125 Exec('NET.EXE',param+' "IIS ADMIN"',
126 ExpandConstant('{sys}'),SW_SHOW,ewWaitUntilTerminated,resultcode
127 );
128end;
129;在IIS默认站点下添加虚拟目录
130procedure CurStepChanged(CurStep: TSetupStep);
131begin
132 case CurStep of
133 ssPostInstall:
134 begin
135 SetupIIS();
136 //ControlIIS(true);
137 end;
138 ssInstall:
139 //ControlIIS(false);
140 else
141 ;
142 end;
143end;
2AppCopyright=test
3AppName=test
4AppVerName=test v2.0
5SolidCompression=true
6OutputDir=Output\
7OutputBaseFilename=test_setup
8DefaultDirName={pf}\Lms
9DefaultGroupName=Lms
10;安装程序的基本信息
11[_ISTool]
12UseAbsolutePaths=false
13
14[UninstallDelete]
15Type: files; Name: {app}\init_test.log
16Type: dirifempty; Name: {app}\database
17;需要提示卸载程序额外删除的目录
18[Run]
19Filename: {tmp}\SetACL.EXE; Parameters: "-ot file -on ""{app}"" -actn ace -ace ""n:S-1-5-20;s:y;p:change,full"""; Flags: runhidden waituntilterminated skipifdoesntexist
20Filename: {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 skipifdoesntexist
21Filename: {tmp}\SetACL.EXE; Parameters: "-ot file -on ""{app}"" -actn ace -ace ""n:S-1-1-0;s:y;p:change,full"""; Flags: runhidden waituntilterminated skipifdoesntexist
22Filename: {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 skipifdoesntexist
23Filename: {tmp}\init_test.exe; Parameters: """{app}"" 2.0"; Description: Configure SQLServer; StatusMsg: Configuring Database; Flags: postinstall skipifdoesntexist
24;uncomment this line to use for dotnet 1.1
25;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: runhidden
26Filename: {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
27;安装过程需要运行的程序,SetACL.EXE是一个第三方组件使用方法大家可以google一下,init_test.exe为附加数据库和数据还有设置asp.net版本的程序
28
29[Dirs]
30Name: {app}\database
31Name: {app}\course
32;产生目录
33[Types]
34Name: Custom; Description: Custom installation; Flags: iscustom
35
36[Files]
37Source: 3rdParty\SetACL.exe; DestDir: {tmp}; flags: deleteafterinstall
38Source: test\*; DestDir: {app}; Excludes:*.webinfo,*.vspscc, \obj,Thumbs.db,CVS,*.pdb,*.cs,*.scc,*.bak,*.csproj,*.log,*.Old,*.user,*.lic,*.sln,*.suo,8.rar; Flags: recursesubdirs
39Source: test_table.sql; DestDir: {tmp}; Flags: deleteafterinstall
40Source: test_Data.sql; DestDir: {tmp}; Flags: deleteafterinstall
41Source: init_test.exe; DestDir: {tmp}; flags: deleteafterinstall
42;需要随安装包一起打包的文件
43[Code]
44const
45 VDirName = 'test';
46 Vwebctrl = 'webctrl_client';
47 IISServerNumber = '1';
48
49function SafeCreateOleObject(ProgId:String;ExceptionMsg:String):Variant;
50var
51 retobj:Variant;
52begin
53 try
54 retobj := CreateOleObject(ProgId);
55 except
56 RaiseException(ExceptionMsg+''#13#13'(Error ''' + GetExceptionMessage + ''' occurred)');
57 end;
58 Result:=retobj;
59end;
60
61{
62 create virtual directory, pointing to installation directory
63}
64
65procedure SetupIIS;
66var
67 IIS, WebSite, WebServer, WebRoot, VDir: Variant;
68begin
69 { Create the main IIS COM Automation object }
70 IIS:=SafeCreateOleObject('IISNamespace','Please install Microsoft IIS first.');
71 { Connect to the IIS server }
72
73 WebSite := IIS.GetObject('IIsWebService', GetComputerNameString() + '/w3svc');
74 WebServer := WebSite.GetObject('IIsWebServer', IISServerNumber);
75 WebRoot := WebServer.GetObject('IIsWebVirtualDir', 'Root');
76
77 { (Re)create a virtual dir }
78
79 try
80 WebRoot.Delete('IIsWebVirtualDir', VDirName);
81 except
82 end;
83 try
84 WebRoot.Delete('IIsWebVirtualDir', Vwebctrl);
85 except
86 end;
87
88 If DirExists(ExpandConstant('{app}')) then
89 begin
90 VDir := WebRoot.Create('IIsWebVirtualDir', VDirName);
91 VDir.AccessRead := True;
92 VDir.AccessFlags:=529;
93 VDir.AppFriendlyName := 'LMS Website';
94 VDir.Path := ExpandConstant('{app}\');
95 VDir.EnableDirBrowsing:=False;
96 VDir.EnableDefaultDoc:=True;
97 VDir.DefaultDoc :='Default.aspx';
98 VDir.AppCreate(True);
99 VDir.SetInfo();
100 end;
101
102 If DirExists(ExpandConstant('{app}\IEWebControl\'+Vwebctrl)) then
103 begin
104 VDir := WebRoot.Create('IIsWebVirtualDir', Vwebctrl);
105 VDir.AccessRead := True;
106 VDir.AccessFlags:=529;
107 VDir.AppFriendlyName := 'visual web ctral';
108 VDir.Path := ExpandConstant('{app}\IEWebControl\'+Vwebctrl);
109 VDir.EnableDirBrowsing:=False;
110 VDir.EnableDefaultDoc:=True;
111 VDir.DefaultDoc :='default.htm';
112 VDir.AppCreate(True);
113 VDir.SetInfo();
114 end;
115end;
116procedure ControlIIS(bState:boolean);
117var
118 resultcode:integer;
119 param:string;
120begin
121 if bState then
122 param:='START'
123 else param:='STOP';
124
125 Exec('NET.EXE',param+' "IIS ADMIN"',
126 ExpandConstant('{sys}'),SW_SHOW,ewWaitUntilTerminated,resultcode
127 );
128end;
129;在IIS默认站点下添加虚拟目录
130procedure CurStepChanged(CurStep: TSetupStep);
131begin
132 case CurStep of
133 ssPostInstall:
134 begin
135 SetupIIS();
136 //ControlIIS(true);
137 end;
138 ssInstall:
139 //ControlIIS(false);
140 else
141 ;
142 end;
143end;
再来看一下init_lms.exe的主要方法:
1private void CreateDatabase()
2 {
3 string sql = " CREATE DATABASE "
4 + dbName;
5
6 if (IsLocalInstall())
7 {
8 sql += " ON PRIMARY (NAME = " + dbDataName + ", "
9 + " FILENAME = '" + getDir("database\\" + dbDataName + ".mdf") + "', "
10 + " SIZE = 5MB,"
11 + " FILEGROWTH =1) "
12 + " LOG ON (NAME =" + dbLogName + ", "
13 + " FILENAME = '" + getDir("database\\" + dbLogName + ".ldf") + "', "
14 + " SIZE = 1MB, "
15 + " FILEGROWTH =1) ";
16 }
17 sql += " COLLATE Chinese_PRC_CI_AS";
18 try
19 {
20 this.Cursor = Cursors.WaitCursor;
21 frmProg.StepText("drop database");
22 if (chkCreateDB.Checked)
23 {
24 try
25 {
26 execSQL("master", "DROP DATABASE " + dbName);
27 writeLog("old database dropped");
28 }
29 catch (Exception)
30 {
31 writeLog("database not found or unable to be dropped");
32 }
33
34 }
35 frmProg.StepText("creating database");
36 if (chkCreateDB.Checked)
37 {
38 try
39 {
40 execSQL("master", sql);
41 writeLog("DB CREATED:" + sql);
42 }
43 catch (Exception e)
44 {
45 writeLog("DB CREATING ERROR:" + e.Message);
46 }
47 }
48
49 frmProg.StepText("creating tables");
50
51 if (chkTables.Checked)
52 {
53 writeLog("exec lmsdb_table.sql");
54 dmoExecSQL(sqlb_t.ToString());//dbName, sqlb);
55 }
56
57 frmProg.StepText("initialize base data");
58
59 if (chkDatas.Checked)
60 {
61 //writeLog(sqlb.ToString());
62 writeLog("exec lmsbasedata.sql");
63 dmoExecSQL(sqlb_d.ToString());//execSQL(dbName, sqlb);
64 }
65 frmProg.StepText("database initialization is done");
66 }
67 finally
68 {
69 this.Cursor = Cursors.Default;
70 }
71 }
2 {
3 string sql = " CREATE DATABASE "
4 + dbName;
5
6 if (IsLocalInstall())
7 {
8 sql += " ON PRIMARY (NAME = " + dbDataName + ", "
9 + " FILENAME = '" + getDir("database\\" + dbDataName + ".mdf") + "', "
10 + " SIZE = 5MB,"
11 + " FILEGROWTH =1) "
12 + " LOG ON (NAME =" + dbLogName + ", "
13 + " FILENAME = '" + getDir("database\\" + dbLogName + ".ldf") + "', "
14 + " SIZE = 1MB, "
15 + " FILEGROWTH =1) ";
16 }
17 sql += " COLLATE Chinese_PRC_CI_AS";
18 try
19 {
20 this.Cursor = Cursors.WaitCursor;
21 frmProg.StepText("drop database");
22 if (chkCreateDB.Checked)
23 {
24 try
25 {
26 execSQL("master", "DROP DATABASE " + dbName);
27 writeLog("old database dropped");
28 }
29 catch (Exception)
30 {
31 writeLog("database not found or unable to be dropped");
32 }
33
34 }
35 frmProg.StepText("creating database");
36 if (chkCreateDB.Checked)
37 {
38 try
39 {
40 execSQL("master", sql);
41 writeLog("DB CREATED:" + sql);
42 }
43 catch (Exception e)
44 {
45 writeLog("DB CREATING ERROR:" + e.Message);
46 }
47 }
48
49 frmProg.StepText("creating tables");
50
51 if (chkTables.Checked)
52 {
53 writeLog("exec lmsdb_table.sql");
54 dmoExecSQL(sqlb_t.ToString());//dbName, sqlb);
55 }
56
57 frmProg.StepText("initialize base data");
58
59 if (chkDatas.Checked)
60 {
61 //writeLog(sqlb.ToString());
62 writeLog("exec lmsbasedata.sql");
63 dmoExecSQL(sqlb_d.ToString());//execSQL(dbName, sqlb);
64 }
65 frmProg.StepText("database initialization is done");
66 }
67 finally
68 {
69 this.Cursor = Cursors.Default;
70 }
71 }
这个方法主要是创建数据库文件
1private void setAspNetVersion(string m,string i)
2 {
3 string dotnetdir=addBackSlash(Environment.GetEnvironmentVariable("windir"))+"Microsoft.Net";
4 string[] dirs = Directory.GetDirectories(dotnetdir+"\\Framework\\");
5 foreach (string d in dirs)
6 {
7 int p = d.LastIndexOf("\\v");
8 if (p >= 0)
9 {
10 string v = d.Substring(p + 2);
11 aspver = v;
12 string[] mi = v.Split(new char[] { '.' });
13 if (mi[0].CompareTo(m)==0 && mi[1].CompareTo(i)>=0)
14 {
15 //found the directory
16 string regiis = d + "\\aspnet_regiis.exe";
17 if (File.Exists(regiis)){
18 Process proc=new Process();
19 try{
20 proc.StartInfo.FileName=regiis;
21 proc.StartInfo.Arguments = "-s W3SVC/1/ROOT/Lms";
22 proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
23 proc.StartInfo.CreateNoWindow=true;
24 proc.Start();
25 proc.WaitForExit(10000);
26 }catch (Win32Exception e){
27
28 if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
29 {
30 Console.WriteLine(e.Message + ". Check the path.");
31 } else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
32 {
33 Console.WriteLine(e.Message +
34 ". You do not have permission to run.");
35 }
36 } //catch
37 }//if file.exists
38 }//if compare version
39 }//if pos>=0
40 }//for each
41
42 }
2 {
3 string dotnetdir=addBackSlash(Environment.GetEnvironmentVariable("windir"))+"Microsoft.Net";
4 string[] dirs = Directory.GetDirectories(dotnetdir+"\\Framework\\");
5 foreach (string d in dirs)
6 {
7 int p = d.LastIndexOf("\\v");
8 if (p >= 0)
9 {
10 string v = d.Substring(p + 2);
11 aspver = v;
12 string[] mi = v.Split(new char[] { '.' });
13 if (mi[0].CompareTo(m)==0 && mi[1].CompareTo(i)>=0)
14 {
15 //found the directory
16 string regiis = d + "\\aspnet_regiis.exe";
17 if (File.Exists(regiis)){
18 Process proc=new Process();
19 try{
20 proc.StartInfo.FileName=regiis;
21 proc.StartInfo.Arguments = "-s W3SVC/1/ROOT/Lms";
22 proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
23 proc.StartInfo.CreateNoWindow=true;
24 proc.Start();
25 proc.WaitForExit(10000);
26 }catch (Win32Exception e){
27
28 if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
29 {
30 Console.WriteLine(e.Message + ". Check the path.");
31 } else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
32 {
33 Console.WriteLine(e.Message +
34 ". You do not have permission to run.");
35 }
36 } //catch
37 }//if file.exists
38 }//if compare version
39 }//if pos>=0
40 }//for each
41
42 }
这个方法主要是利用aspnet_regiis.exe来修改ASP.NET的版本,这个文件在Microsoft.Net目录下