C#如何打包EXE程序生成setup安装文件

Inno Setup 是一个免费的安装制作软件 (开源) ,它既有适合新手的脚本向导,又有良好的脚本编辑环境,能够快速制作出标准 Windows2000 风格 的安装界面,足以完成一般的安装任务,在新版本版中软件加入了对多语言的支持,这个功能的实现相较于 NSIS 来得简单多了, 推荐大家使用新的版本。.

包括以下功能

1 宏定义#define

自定义一些全局变量,可以在下文中用到。

2 [Setup]

设置安装包名称、作者等基础信息。

3 [Languages]

设置安装语言。

4 [Types]

安装类型设置。

5 [Components]

组件选择(可选择)。

6 [Tasks]

附加任务执行(可选择)。

7 [Files]

文件安装。

8 [Icons]

开始菜单,桌面快捷方式。

9 [Run]

文件安装完成后,运行程序,像bat命令和exe执行程序。

10 [Messages]

更改显示在程序中显示的消息文本。

11 [INI]

用于在用户系统中创建,修改或删除ini文件健值

12 [Registry]

用于在用户系统中创建,修改或删除注册表健值

13 [code]

在执行脚本

下面就是一个完整的.iss脚本文件。

; 脚本由 Inno Setup 脚本向导 生成!
; 有关创建 Inno Setup 脚本文件的详细资料请查阅帮助文档!#define MyAppName "xxxx"#define MyAppVerName "xxxx 1.0"#define MyAppPublisher "xxxx科技有限公司"#define MyAppURL "http://faxin.cn/"
#define MyAppExeName "xxxx.exe"

[Setup]
; 注: AppId的值为单独标识该应用程序。
; 不要为其他安装程序使用相同的AppId值。
; (生成新的GUID,点击 工具|在IDE中生成GUID。)
AppId={{34862BE5-9DCF-444F-997A-EC9883F49981}
AppName={#MyAppName}
AppVerName={#MyAppVerName}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={pf}\{#MyAppName}
DefaultGroupName={#MyAppName}
AllowNoIcons=yes
LicenseFile=D:\工作代码\wps\wpfSoftware\文档\1.txt
InfoBeforeFile=D:\工作代码\wps\wpfSoftware\文档\2.txt
InfoAfterFile=D:\工作代码\wps\wpfSoftware\文档\3.txt
OutputDir=C:\Users\conan\Desktop
OutputBaseFilename=wszs_setup
SetupIconFile=D:\工作代码\wps\wpfSoftware\src\Document_from\dongfa.ico
Compression=lzma
SolidCompression=yes

[Languages]
Name: "chinesesimp"; MessagesFile: "compiler:Default.isl"

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: checkablealone
Name: "quicklaunchicon"; Description: "{cm:CreateQuickLaunchIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: checkablealone

[Files]
Source: "C:\Debug\文书助手.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Debug\PlugInstallation.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Debug\PlugInstallation.exe.config"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Debug\SideBar.dll"; DestDir: "{app}"; Flags: ignoreversion

Source: "C:\Debug\PlugInstallation.pdb"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Debug\jsplugins.xml"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Debug\YuanFeng.LegalDoc.AddOns.Wps_1.1.0\*"; DestDir: "{app}\YuanFeng.LegalDoc.AddOns.Wps_1.1.0"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "C:\Debug\AutoUpdater.Desktop.SDK.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Debug\AutoUpdater.Desktop.SDK.pdb"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Debug\Newtonsoft.Json.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Debug\Nlog.config"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Debug\NLog.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Debug\NLog.xml"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Debug\RestSharp.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Debug\RestSharp.xml"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Debug\文书助手.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Debug\文书助手.exe.config"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Debug\文书助手.pdb"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Debug\localhtml\*"; DestDir: "{app}\localhtml\"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "C:\Debug\Img\*"; DestDir: "{app}\Img\"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "C:\Debug\updater\*"; DestDir: "{app}\updater\"; Flags: ignoreversion recursesubdirs createallsubdirs
; 注意: 不要在任何共享系统文件上使用“Flags: ignoreversion”

[Icons]
Name: "{group}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"Name: "{group}\{cm:ProgramOnTheWeb,{#MyAppName}}"; Filename: "{#MyAppURL}"Name: "{group}\{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}"Name: "{commondesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon
Name: "{userappdata}\Microsoft\Internet Explorer\Quick Launch\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: quicklaunchicon
Name: "{commondesktop}\文书助手WPS插件修复"; Filename: "{app}\PlugInstallation.exe"; Tasks: desktopicon

[Messages]
;卸载对话框说明
ConfirmUninstall=您真的想要从电脑中卸载文书助手吗?%n%n按 [是] 将会删除 %1 以及它的组件;%n按 [否] 则让软件继续留在您的电脑上.[Run]Filename: "{app}\PlugInstallation.exe";Description:"初始化程序"; StatusMsg:"别急,等一下就好"Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#MyAppName}}"; Flags: nowait postinstall skipifsilent

[Registry]  
Root: HKLM ;SubKey:"Software\ZhugAssistant";ValueType:dword;ValueName:config;ValueData:10 ;Flags: uninsdeletekey

[code]
//;通过名称终结进程
procedure TaskKillProcessByName(AppName: String);var
  WbemLocator : Variant;
  WMIService   : Variant;
  WbemObjectSet: Variant;
  WbemObject   : Variant;
begin;
  WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  WMIService := WbemLocator.ConnectServer('localhost', 'root\CIMV2');
  WbemObjectSet := WMIService.ExecQuery('SELECT * FROM Win32_Process Where Name="' + AppName + '"');
  if not VarIsNull(WbemObjectSet) and (WbemObjectSet.Count > 0) then
  begin
    WbemObject := WbemObjectSet.ItemIndex(0);
    if not VarIsNull(WbemObject) then
    begin
      WbemObject.Terminate();
      WbemObject := Unassigned;
    end;
  end;
end;//init setupfunction InitializeSetup(): boolean;  
//varvar Istoken: boolean; 
//avoid setup again   begin       
Istoken := true;   
if RegValueExists(HKEY_LOCAL_MACHINE, 'SOFTWARE\ZhugAssistant', 'config') then  
begin  
  //  MsgBox('程序已安装,请卸载后再尝试安装!',mbConfirmation, MB_OK);
  //  Istoken := false;
    

     begin
    if MsgBox('安装程序检测到 {#MyAppName} 正在运行!'#13''#13'单击“是”按钮关闭程序并继续安装;'#13''#13'单击“否”按钮退出安装!', mbConfirmation, MB_YESNO) = IDYES then
    begin      TaskKillProcessByName('{#MyAppExeName}');      TaskKillProcessByName('{#MyAppExeName}');
       Istoken := true;
    end
    else
       Istoken := false;
  end;

    
    
end else  
begin  
     Istoken := true;  
end;  
Result := Istoken;  
end;//next buttonfunction NextButtonClick(CurPageID: Integer): Boolean;  
//varvar ResultCode: Integer;  
var IsSetup : Boolean;  
var Isnet : Boolean;  
var HistoryBufferSize: Cardinal;var RegeditValue: Integer;  
var RegeditValue2: String; 
var ResultCode2: Integer;  
var Iswps : Boolean;  
begin  
IsSetup := true ;  
Isnet := true ;  
Iswps := true ;  
 RegeditValue2 := '';
 ResultCode2 := 0 ;  
case CurPageID of  
    wpReady:  
      begin  
      if RegQueryDWordValue(HKLM, 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\full','release',HistoryBufferSize) then
begin
    RegeditValue:=HistoryBufferSize;    if(RegeditValue>=394254) then
    begin 
   //   Result:=true;
    end
    else 
      begin
        if MsgBox('检测到系统没有安装.Net Framework4.6.1!'+#13#10#13#10 +'是否退出此次安装?',mbInformation,MB_YESNO)=IDYES then
          begin          //  Result:=false;
           Isnet:=false;
          end
        else
          begin           //  Result:=true;
               MsgBox('程序执行需要NET的支持,安装可能需要2、3分钟,开始安装',mbConfirmation, MB_OK);           //  ExtractTemporaryFile('NDP461-KB3102436-x86-x64-AllOS-ENU.exe');  
     //  Exec(ExpandConstant('{tmp}\NDP461-KB3102436-x86-x64-AllOS-ENU.exe'), '', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode)
   
          end
      end
    end 
else
begin
    if MsgBox('检测到系统没有安装.Net Framework4.6.1!'+#13#10#13#10 +'是否退出此次安装?',mbInformation,MB_YESNO)=IDYES then
    begin   //   Result:=false;
     Isnet:=false;
    end
    else
    begin    //   Result:=true;
      MsgBox('程序执行需要NET的支持,安装可能需要2、3分钟,开始安装',mbConfirmation, MB_OK);    //  ExtractTemporaryFile('NDP461-KB3102436-x86-x64-AllOS-ENU.exe');
    //   Exec(ExpandConstant('{tmp}\NDP461-KB3102436-x86-x64-AllOS-ENU.exe'), '', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode)
   
    end
end;


  if Isnet then
    begin 

    end
       else
    begin    //  IsSetup := false ;  
    end; // 测试暂时不管wps
   Iswps := false ;   //

  if IsSetup then
    begin 
 
      // if not RegValueExists(HKEY_LOCAL_MACHINE, 'SOFTWARE\Classes\CLSID\{6F3DD387-5AF2-492B-BDE2-30FF2F451241}\InprocServer32\12.0.0.0', 'RuntimeVersion') then   begin  
          if Iswps then   begin  
        
     //   if MsgBox('程序执行需要W.P.S的支持,是否现在安装?', mbConfirmation, MB_YESNO) = idYes then begin  
          if Iswps then begin  

        MsgBox('程序执行需要W.P.S的支持,安装可能需要2、3分钟,开始安装',mbConfirmation, MB_OK);           ExtractTemporaryFile('WPS_Setup_11744.exe');
         
         if not Exec(ExpandConstant('{tmp}\WPS_Setup_11744.exe'), '', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode) then  
              MsgBox('WPS安装出错:' #13#13 ' ' + SysErrorMessage(ResultCode) + '.', mbError, MB_OK);
          end else begin  
                IsSetup := false ;  
          end ;  

          BringToFrontAndRestore();  
        end;  

      end;  
      
    
    end
       else
    begin
  
    end;
end;  
Result := IsSetup;  
end;

function InitializeUninstall(): Boolean;var appWnd: HWND;
begin   // 检查**进程是否在运行,是则关闭进程
   appWnd := FindWindowByWindowName('文书助手');
   if (appWnd <> 0) then
   begin
  if MsgBox('卸载程序检测到 {#MyAppName} 正在运行!'#13''#13'单击“是”按钮关闭程序并继续卸载;'#13''#13'单击“否”按钮退出卸载!', mbConfirmation, MB_YESNO) = IDYES then
          begin            PostMessage(appWnd, 18, 0, 0);                 // 退出主进程
            DelTree(ExpandConstant('{app}'), True, True, True); 
          Result:= true;
          end
        else
          begin
          Result:= false;
            end; 
 end
      else
    begin
   Result :=True;
    end;
end;