嵌入C#程序内外部的可执行文件 [英] Embedding an external executable inside a C# program

查看:160
本文介绍了嵌入C#程序内外部的可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何嵌入我的C#Windows内部的外部可执行窗体应用程序?

How do I embed an external executable inside my C# Windows Forms application?

编辑:我需要嵌入它,因为它是一个免费的外部控制台应用程序(在C ++做)从中我读到的输出值在我的程序中使用。这将是很好的,更专业的要把它嵌入。

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.

第二个原因是嵌入.NET应用程序中的Flash放映文件的要求。

Second reason is a requirement to embed a Flash projector file inside a .NET application.

推荐答案

下面是一些示例code,将大致实现这一目标,减去任何形式的错误检查。此外,请确保要嵌入程序的许可证允许这种使用。

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.

// extracts [resource] into the the file specified by [path]
void ExtractResource( string resource, string path )
{
    Stream stream = GetType().Assembly.GetManifestResourceStream( resource );
    byte[] bytes = new byte[(int)stream.Length];
    stream.Read( bytes, 0, bytes.Length );
    File.WriteAllBytes( path, bytes );
}

string exePath = "c:\temp\embedded.exe";
ExtractResource( "myProj.embedded.exe", exePath );
// run the exe...
File.Delete( exePath );

唯一棘手的部分越来越为第一个参数 ExtractResource 正确的价值。它应该有形式namespace.name,其中命名空间为您的项目的默认命名空间(发现这个项目下|地产|应用|默认命名空间)。第二部分是文件,你需要在你的项目包括(请务​​必设置构建选项为嵌入的资源)的名称。如果你把该文件的目录下,例如资源,那么该名称将成为资源名称(如myProj.Resources.Embedded.exe)的一部分。如果您遇到麻烦,试着在反射器打开你的编译的二进制,并期待在资源文件夹。这里列出的名字,你会传递给 GetManifestResourceStream 的名称。

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 to GetManifestResourceStream.

这篇关于嵌入C#程序内外部的可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆