在 C# 程序中嵌入外部可执行文件
如何在我的 C# Windows 窗体应用程序中嵌入外部可执行文件?
编辑:我需要嵌入它,因为它是一个外部免费控制台应用程序(用 C++ 编写),我从中读取要在程序中使用的输出值。 如果能嵌入它就更好了,也更专业。
第二个原因是需要将 Flash 投影仪文件嵌入到 .NET 应用程序中。
How do I embed an external executable inside my C# Windows Forms application?
Edit: I need to embed it because it's an external free console application (made in C++) from which I read the output values to use in my program. It would be nice and more professional to have it embedded.
Second reason is a requirement to embed a Flash projector file inside a .NET application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
最简单的方法,从 Will 所说的开始:
编码:
Simplest way, leading on from what Will said:
Code this:
下面是一些示例代码,可以大致完成此任务,减去任何类型的错误检查。 另外,请确保要嵌入的程序的许可证允许此类使用。
唯一棘手的部分是为
ExtractResource
的第一个参数获取正确的值。 它的格式应为“namespace.name”,其中namespace 是项目的默认命名空间(可在“项目”|“属性”|“应用程序”|“默认命名空间”下找到)。 第二部分是文件的名称,您需要将其包含在项目中(确保将构建选项设置为“嵌入式资源”)。 如果将文件放在某个目录(例如 Resources)下,则该名称将成为资源名称的一部分(例如“myProj.Resources.Embedded.exe”)。 如果遇到问题,请尝试在 Reflector 中打开已编译的二进制文件并查看 Resources 文件夹。 此处列出的名称是您要传递给GetManifestResourceStream
的名称。Here is some sample code that would roughly accomplish this, minus error checking of any sort. Also, please make sure that the license of the program to be embedded allows this sort of use.
The only tricky part is getting the right value for the first argument to
ExtractResource
. It should have the form "namespace.name", where namespace is the default namespace for your project (find this under Project | Properties | Application | Default namespace). The second part is the name of the file, which you'll need to include in your project (make sure to set the build option to "Embedded Resource"). If you put the file under a directory, e.g. Resources, then that name becomes part of the resource name (e.g. "myProj.Resources.Embedded.exe"). If you're having trouble, try opening your compiled binary in Reflector and look in the Resources folder. The names listed here are the names that you would pass toGetManifestResourceStream
.只需将其添加到您的项目并将构建选项设置为“嵌入式资源”
Just add it to your project and set the build option to "Embedded Resource"
这可能是最简单的:
This is probably the simplest:
可执行文件是托管程序集吗? 如果是这样,您可以使用 ILMerge 将该程序集与您的程序集合并。
Is the executable a managed assembly? If so you can use ILMerge to merge that assembly with yours.
这是我的版本:
将文件作为现有项目添加到项目中,将文件的属性更改为“嵌入资源”
要将文件动态提取到给定位置:(此示例不测试位置的写入权限等)
Here's my version:
Add the file to the project as an existing item, change the properties on the file to "Embedded resource"
To dynamically extract the file to a given location: (this example doesn't test location for write permissions etc)
您的代码存在资源错误(文件句柄未释放!),请更正为:
Your code has resource bug (file handle not freed!), please correct to:
如果需要,将某些内容提取为字符串:
Extract something as string, if needed: