制作将使用 Windows Temp 目录的自解压 zip (SFX) Windows 程序安装程序的最简单方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8088233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the easiest way to make a self-extracting zip (SFX) Windows program installer that will use the Windows Temp directory?
提问by jpeskin
I have a very simple program that consists of a .NET 2.0 exe (Program.exe) that calls an x86 Win32 .dll (Lib.dll).
我有一个非常简单的程序,它由一个调用 x86 Win32 .dll (Lib.dll) 的 .NET 2.0 exe (Program.exe) 组成。
I would like to bundle these into a single self-extracting zip (SFX) called Tool.exe Tool.exe will extract the files (Program.exe and Lib.dll) into the Windows Temp system directory, and then call Program.exe
我想将它们捆绑到一个名为 Tool.exe 的自解压 zip (SFX) 中 Tool.exe 会将文件(Program.exe 和 Lib.dll)解压缩到 Windows Temp 系统目录中,然后调用 Program.exe
This way I can offer a single-file .exe download called Tool.exe and as far as the user is concerned they are just running Tool.exe and not a multi-file program.
通过这种方式,我可以提供一个名为 Tool.exe 的单文件 .exe 下载,就用户而言,他们只是运行 Tool.exe 而不是多文件程序。
WinRAR has SFX capabilities, and ability to auto-launch an extracted .exe, but it doesn't seem to give you the option to let it extract to the Windows Temp dir (you can specify absolute path, but the Temp dir varies depending on what version of Windows). Also, it pops up a window when extracting, and that's overkill for my goals of making it appear like the user is just launching my program.
WinRAR 具有 SFX 功能,以及自动启动提取的 .exe 的能力,但它似乎没有让您选择让它提取到 Windows 临时目录(您可以指定绝对路径,但临时目录因什么版本的Windows)。此外,它在提取时会弹出一个窗口,这对于我让它看起来像用户刚刚启动我的程序的目标来说太过分了。
Alternatively, is there a way to bundle the native Lib.dll into my compiled .NET executable, almost like a "resource"?
或者,有没有办法将本机 Lib.dll 捆绑到我编译的 .NET 可执行文件中,就像“资源”一样?
I'd really like to avoid making a full on MSI or even a regular .exe installer as that is a pain to do, even with simpler installers like NSIS.
我真的很想避免在 MSI 甚至常规 .exe 安装程序上进行完整安装,因为即使使用像 NSIS 这样更简单的安装程序,这样做也很痛苦。
回答by ewall
I tend to do this with just 7-zipand UPX, following these directions to make it run a batch file upon execution/extraction. Here's an example CMD script I use to build the EXE including the files in the .\bin directory:
我倾向于只使用7-zip和UPX来做到这一点,按照这些说明使其在执行/提取时运行批处理文件。这是我用来构建 EXE 的示例 CMD 脚本,包括 .\bin 目录中的文件:
pushd %~dp0
upx --ultra-brute 7zsd.sfx
cd Bin
..za a -mx=9 "..\Program.7z" *
cd ..
copy /b 7zsd.sfx + Config.txt + Program.7z Program_Name.exe
del Program.7z
The config.txt file reads like this:
config.txt 文件内容如下:
;!@Install@!UTF-8!
GUIMode="0"
RunProgram="runme.cmd"
;!@InstallEnd@!
Your mileage may vary, of course...
当然,您的里程可能会有所不同...
回答by Charlie Salts
NSIS has a useful utility called Zip2Exe. Basically, you give it a zip file, and specify the extraction folder (in your case, $TEMP
) and it generates an installer.
NSIS 有一个有用的实用程序,称为 Zip2Exe。基本上,您给它一个 zip 文件,并指定提取文件夹(在您的情况下,$TEMP
)并生成一个安装程序。
You can run such an installer in silent mode (so the user does not actually see the installer). Thus, you might be able to have Tool.exe
install and run the program for you. The installer can be an embedded resource which you first write to a temp file before running. Then, the user never knows about any installer. From their point of view, Tool.exe
does some work, gives some "please wait..." feedback, and then the actual program you want to run is started and Tool.exe
quits.
您可以在静默模式下运行这样的安装程序(因此用户实际上看不到安装程序)。因此,您可能能够Tool.exe
为您安装并运行该程序。安装程序可以是一个嵌入式资源,您在运行之前首先将其写入临时文件。然后,用户永远不知道任何安装程序。从他们的角度来看,Tool.exe
做一些工作,给出一些“请稍等...”的反馈,然后你想要运行的实际程序开始并Tool.exe
退出。
Edit: In hindsight, you might find it's easier to just extract and run Program.exe
(and your dll) from Tool.exe
, and then quit Tool.exe
. Better still: Let the user run Program.exe
(which writes lib.dll
to disk before loading it).
编辑:事后看来,您可能会发现从 中提取并运行Program.exe
(和您的 dll)Tool.exe
,然后退出更容易Tool.exe
。更好的是:让用户运行Program.exe
(lib.dll
在加载之前写入磁盘)。
I hope this gives you some ideas. Be aware you might have trouble with people's UAC and virus scanners.
我希望这能给你一些想法。请注意,您可能在使用人们的 UAC 和病毒扫描程序时遇到问题。
回答by Cheeso
DotNetZip can produce an SFX, with a default extract directory. When you create the SFX, specify %TEMP% as the default extract location, and when the SFX is actually run, the variable gets resolved. DotNetZip can also run a program after extracting the SFX, and that program can be one of the things that got extracted.
DotNetZip 可以生成带有默认提取目录的 SFX。创建 SFX 时,将 %TEMP% 指定为默认提取位置,当 SFX 实际运行时,变量会得到解析。DotNetZip 还可以在提取 SFX 后运行程序,该程序可以是提取的内容之一。
So you could use DotNetZip to do what you want.
所以你可以使用 DotNetZip 来做你想做的事。
An alternative is explained in this article. It shows how to dynamically make calls into native Win32 DLLs from .NET. Using that technique, you could bundle the library as a resource into your .NET EXE, and then when the .NET EXE starts, extract the Library, and then make the dynamic calls into it.
本文解释了另一种方法。它展示了如何从 .NET 动态调用本机 Win32 DLL。使用该技术,您可以将库作为资源捆绑到 .NET EXE 中,然后在 .NET EXE 启动时提取库,然后对它进行动态调用。