运行exe文件在C#中嵌入的资源 [英] Run Exe file as an Embedded Resource in C#

查看:272
本文介绍了运行exe文件在C#中嵌入的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个第三方的EXE。我只是需要从我的C#应用​​程序运行此。



我的首要目标就是版权,从我的C#文件,第三方可执行..



有没有更好的办法做到这一点?



我怎样才能做到这一点?



感谢您

Menaka


解决方案

  1. 首先,嵌入式可执行文件的资源文件添加到您现有的资源文件,如果你没有一个,那么你需要[添加现有项目到项目,并选择资源文件]

  2. 当您在资源编辑器页面中添加可执行文件,选择类型为[文件],然后找到你的嵌入式excutable文件,并添加它。例如,文件命名为subexe.exe,那么资源设计CS文件将有下面的代码添加:结果

     内部静态的byte [] {SubExe 
    获得{
    obj对象= ResourceManager.GetObject(SubExe,resourceCulture);
    回报率((字节[])(OBJ));
    }
    }


  3. 添加到访问你的资源的方法,这也很简单,只需添加以下代码到你的资源的设计师CS文件

      
    公共静态的byte [] GetSubExe()
    {
    返回SubExe;
    }


  4. 在您的主执行程序的源代码,添加以下阅读资源,并将其写入到一个新文件

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

     使用(的FileStream fsDst =新的FileStream(tempExeName,FileMode.CreateNew,FileAccess.Write))
    {
    字节[]字节= Resource1.GetSubExe();

    fsDst.Write(字节,0,bytes.Length);
    }




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



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

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

Is there any better way to do this.?

How can I do this ?

Thank you
Menaka

解决方案

  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

这篇关于运行exe文件在C#中嵌入的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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