18

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 ?

Osama AbuSitta
  • 3,918
  • 4
  • 35
  • 51
menakacol
  • 181
  • 1
  • 1
  • 5

3 Answers3

23
  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

ljh
  • 2,546
  • 1
  • 14
  • 20
  • As you required, I update the answer. If you need full code, send me your personal email, I will reply with full code. – ljh Mar 18 '13 at 07:39
  • 3
    This code would make the exe vunerable while running though. I could just check what process is running, go to the folder and then copy the exe and be rid of the wrapper. So this really isn't that secure. – Karl-Johan Sjögren Mar 23 '13 at 09:55
  • Definitely agree, should run it directly in memory, but honestly I don't know how to execute it in memory. – ljh Mar 23 '13 at 10:02
1

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();
      }
}
Alik Khilazhev
  • 995
  • 6
  • 18
-1

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

private void button_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("fire.EXE");
        }
Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
EAGLEONE
  • 35
  • 1
  • 3
    Not mine, but the down votes are probably because this does NOT answer the OP. Regardless the comments saying `add your exe to your project`, the code runs the selected program based on the PATH, which _may_ not get the one you want. Especially since there is no mention of flagging the files you just added to your project as `Copy to Output`. Also, the comments in the OP indicate protecting the files from direct execution, even if this code is fixed, that is not addressed. – Jesse Chisholm Feb 03 '15 at 22:00