在 C# 中即时解压缩
我已经构建了一个控制台应用程序,当它引用 Program Files 中的 .exe 文件时,该应用程序可以正常工作,但我的用户在其 Program Files 目录中可能没有该 .exe 文件。
为了简单起见,我更愿意将包保留为单个 .exe,因此我想知道如何将两个 .exe 合并到一个包中。
我想到的一件事是将 .exe 从 Program Files 目录压缩到临时位置,然后将 zip 存档的二进制数据存储在我的控制台应用程序源代码中。这是最好的方法还是有更好的方法?
请注意,我没有要在控制台应用程序中引用的 .exe 的源代码。
I have built a console application that works okay when it references a .exe file from a Program Files, but my users may not have that .exe in their Program Files directory.
I would prefer to keep the package as a single .exe for simplicity, so I was wondering how I can combine the two .exe's into one package.
One thing I thought of is zipping the .exe from the Program Files directory to a temporary location, and I would store the binary data for the zip archive in my console applications source code. Is this the best way to do it or are there better methods?
Note I do not have the source code of the .exe I want to reference in my console application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您当然可以将额外的文件作为嵌入式资源存储在 .exe(或 .dll)中。只需将项目中项目的“构建操作”更改为“嵌入式资源”即可。您可以使用以下命令检索文件的内容(如果您愿意,可以压缩该文件):
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("stream name")
您可以将文件提取到磁盘上以便能够引用它,也可以直接使用其中一个程序集加载它。 Load() 变体,因此您不需要将其存储在磁盘上。
请注意,如果您选择提取它并将其存储在磁盘上,则需要 Vista 和 Windows 7(以及正确管理的 XP)操作系统的管理员权限才能将文件保存到 Program Files 目录。
You can certainly store extra files in your .exe (or .dll) as embedded resources. Simply change the "build action" for the item in the project to "Embedded Resource". You can retrieve the contents of the file (which could be compressed, if you wished) by using the following:
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("stream name")
You could extract the file onto disk to be able to reference it, or you could load it directly with one of the Assembly.Load() variants, so you wouldn't need to ever store it on disk.
Note that if you do choose to extract it and store it on disk, you'll need administrator permissions on Vista and Windows 7 (and properly administered XP) operating systems in order to save the file(s) to the Program Files directory.
您可以使用 GZipStream 来压缩和解压缩C# 中的文件。对于更复杂的压缩,您可以使用其他 Zip 库,例如 SharpZipLib。
You can use GZipStream to compress and decompress files in C#. For more complex compression, you can use other Zip libraries like SharpZipLib.
看一下这个链接: 将程序集嵌入另一个程序集
基本上,ILMerge 会做什么你在问。
Take a look at this link: Embedding assemblies inside another assembly
Basically, ILMerge will do what you are asking.