39

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

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

第二个原因是需要在 .NET 应用程序中嵌入 Flash 投影仪文件。

4

8 回答 8

61

最简单的方法,从威尔所说的开始:

  1. 使用 Resources.resx 添加 .exe
  2. 代码如下:

    string path = Path.Combine(Path.GetTempPath(), "tempfile.exe");  
    File.WriteAllBytes(path, MyNamespace.Properties.Resources.MyExecutable);
    Process.Start(path);
    
于 2012-03-15T12:02:37.430 回答
25

这是一些可以大致完成此任务的示例代码,减去任何类型的错误检查。另外,请确保要嵌入的程序的许可证允许这种使用。

// 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”的形式,其中命名空间是您项目的默认命名空间(在项目 | 属性 | 应用程序 | 默认命名空间下找到它)。第二部分是文件的名称,您需要将其包含在项目中(确保将构建选项设置为“嵌入式资源”)。如果您将文件放在一个目录下,例如 Resources,那么该名称将成为资源名称的一部分(例如“myProj.Resources.Embedded.exe”)。如果您遇到问题,请尝试在 Reflector 中打开已编译的二进制文件并查看 Resources 文件夹。此处列出的名称是您将传递给的名称GetManifestResourceStream

于 2009-04-28T17:23:42.063 回答
14

只需将其添加到您的项目并将构建选项设置为“嵌入式资源”

于 2009-04-28T16:01:22.573 回答
8

这可能是最简单的:

byte[] exeBytes = Properties.Resources.myApp;
string exeToRun = Path.Combine(Path.GetTempPath(), "myApp.exe");

using (FileStream exeFile = new FileStream(exeToRun, FileMode.CreateNew))
    exeFile.Write(exeBytes, 0, exeBytes.Length);

Process.Start(exeToRun);
于 2012-03-04T14:32:17.247 回答
4

可执行文件是托管程序集吗?如果是这样,您可以使用ILMerge将该程序集与您的程序集合并。

于 2009-04-28T15:57:35.360 回答
2

这是我的版本:将文件作为现有项目添加到项目中,将文件上的属性更改为“嵌入式资源”

将文件动态提取到给定位置:(此示例不测试位置的写入权限等)

    /// <summary>
    /// Extract Embedded resource files to a given path
    /// </summary>
    /// <param name="embeddedFileName">Name of the embedded resource file</param>
    /// <param name="destinationPath">Path and file to export resource to</param>
    public static void extractResource(String embeddedFileName, String destinationPath)
    {
        Assembly currentAssembly = Assembly.GetExecutingAssembly();
        string[] arrResources = currentAssembly.GetManifestResourceNames();
        foreach (string resourceName in arrResources)
            if (resourceName.ToUpper().EndsWith(embeddedFileName.ToUpper()))
            {
                Stream resourceToSave = currentAssembly.GetManifestResourceStream(resourceName);
                var output = File.OpenWrite(destinationPath);
                resourceToSave.CopyTo(output);
                resourceToSave.Close();
            }
    }
于 2013-08-25T22:45:33.980 回答
1
  1. 将文件添加到 VS 项目
  2. 标记为“嵌入式资源”-> 文件属性
  3. 使用名称解析:[Assembly Name].[Name of embedded resource] like "MyFunkyNTServcice.SelfDelete.bat"

您的代码有资源错误(文件句柄未释放!),请更正:

public static void extractResource(String embeddedFileName, String destinationPath)
    {
        var currentAssembly = Assembly.GetExecutingAssembly();
        var arrResources = currentAssembly.GetManifestResourceNames();
        foreach (var resourceName in arrResources)
        {
            if (resourceName.ToUpper().EndsWith(embeddedFileName.ToUpper()))
            {
                using (var resourceToSave = currentAssembly.GetManifestResourceStream(resourceName))
                {
                    using (var output = File.OpenWrite(destinationPath))
                        resourceToSave.CopyTo(output);
                    resourceToSave.Close();
                }
            }
        }
    }
于 2015-02-23T13:08:58.023 回答
0

如果需要,将某些内容提取为字符串:

public static string ExtractResourceAsString(String embeddedFileName)
    {
        var currentAssembly = Assembly.GetExecutingAssembly();
        var arrResources = currentAssembly.GetManifestResourceNames();
        foreach (var resourceName in arrResources)
        {
            if (resourceName.ToUpper().EndsWith(embeddedFileName.ToUpper()))
            {
                using (var resourceToSave = currentAssembly.GetManifestResourceStream(resourceName))
                {
                    using (var output = new MemoryStream())
                    {
                        resourceToSave.CopyTo(output);
                        return Encoding.ASCII.GetString(output.ToArray());
                    }
                }
            }
        }

        return string.Empty;
    }
于 2015-02-23T19:40:44.817 回答