在 C# 中将 Exe 文件作为嵌入式资源运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15470090/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:56:33  来源:igfitidea点击:

Run Exe file as an Embedded Resource in C#

c#resourcesembedexe

提问by menakacol

I have a 3rd party EXE. I just need to run this from my C# application.

我有一个第 3 方 EXE。我只需要从我的 C# 应用程序运行它。

My prime target is to copyright that 3rd party executable from my C# file..

我的主要目标是从我的 C# 文件获得第 3 方可执行文件的版权。

Is there any better way to do this.?

有没有更好的方法来做到这一点。?

How can I do this ?

我怎样才能做到这一点 ?

Thank you
Menaka

谢谢
门金加

回答by ljh

  1. First add the embeded executable file as resource file to your existing resource file, if you dont have one, then you need to [add existing item to your project, and select resource file]
  2. When you add the executable file in resource editor page, select type as [Files], then find your embeded excutable file and add it. For example the file named as "subexe.exe", then the resource design cs file will have following code added:
    internal static byte[] SubExe {
            get {
                object obj = ResourceManager.GetObject("SubExe", resourceCulture);
                return ((byte[])(obj));
            }
        }
    
  3. add a method to access to your resource, which is also very simple, just add following code to your resource designer cs file

    
    public static byte[] GetSubExe()
        {
            return SubExe;
        }
    

  4. In your main executable source code, add following to read resource and write it to a new file

    
    string tempExeName = Path.Combine(Directory.GetCurrentDirectory(), "A3E5.exe");

        using(FileStream fsDst = new FileStream(tempExeName,FileMode.CreateNew,FileAccess.Write))
        {
            byte[] bytes = Resource1.GetSubExe();
    
            fsDst.Write(bytes, 0, bytes.Length);
        }    
    

  5. Use process to run the new executable file

  1. 首先将嵌入的可执行文件作为资源文件添加到您现有的资源文件中,如果您没有,则需要[将现有项目添加到您的项目中,并选择资源文件]
  2. 在资源编辑器页面添加可执行文件时,选择类型为[文件],然后找到您嵌入的可执行文件并添加它。例如名为“subexe.exe”的文件,那么资源设计cs文件将添加以下代码:
    internal static byte[] SubExe {
            get {
                object obj = ResourceManager.GetObject("SubExe", resourceCulture);
                return ((byte[])(obj));
            }
        }
    
  3. 添加访问你的资源的方法,这也很简单,只需在你的资源设计器cs文件中添加以下代码

    
    public static byte[] GetSubExe()
        {
            return SubExe;
        }
    

  4. 在您的主要可执行源代码中,添加以下内容以读取资源并将其写入新文件

    
    string tempExeName = Path.Combine(Directory.GetCurrentDirectory(), "A3E5.exe");

        using(FileStream fsDst = new FileStream(tempExeName,FileMode.CreateNew,FileAccess.Write))
        {
            byte[] bytes = Resource1.GetSubExe();
    
            fsDst.Write(bytes, 0, bytes.Length);
        }    
    

  5. 使用进程运行新的可执行文件

回答by EAGLEONE

right click on ur project the solution explorer then add existing item select executable file in dialog box then go to your exe path and add your exe in your project.. then if u wanna start your exe on button click event then write this code its simple easy ...

右键单击您的项目解决方案资源管理器然后在对话框中添加现有项目选择可执行文件然后转到您的exe路径并将您的exe添加到您的项目中。简单 ...

private void button_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("fire.EXE");
        }

回答by alikhil

One could write a simpler

可以写一个更简单的

private void ExtractResource(string resName, string fName)
 {
      object ob = Properties.Resources.ResourceManager.GetObject(resName, originalCulture);
      byte[] myResBytes = (byte[])ob;
      using (FileStream fsDst = new FileStream(fName, FileMode.CreateNew, FileAccess.Write))
      {
         byte[] bytes = myResBytes;
         fsDst.Write(bytes, 0, bytes.Length);
         fsDst.Close();
         fsDst.Dispose();
      }
}