Run Exe file as an Embedded Resource in C#

  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


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();
      }
}