I'm trying to embed some .exe and .dll files into my application (simply by including required files into my project, right-click them and change Build Action to "Embedded Resource"). Here is my code to extract:
private void ExtractTo(string path){
string filename = "MyNamespace.Resources.MyEmbeddedEXEfile.exe";
Assembly asm = Assembly.GetExecutingAssembly();
string[] resourceNames = asm.GetManifestResourceNames();
if(resourceNames.Contains(filename)){
Stream s = asm.GetManifestResourceStream(filename);
byte[] buffer = new byte[s.Length];
File.WriteAllBytes(path + "\\MyEXEfile.exe", buffer);
}
}
It seemed to work well, the embedded file was extracted to the Path passed in, it has the same size to the file I included in my project and embedded as resource. But it seemed to be corrupted, after clicking on the extracted EXE file, an error dialog displayed saying that "The version of this file is not compatible with the version of Windows you're running. Check your computer's system information to see whether you need an x86 (32-bit) or x64 (64-bit) of the program, and then contact your software publisher." Well, I'm exactly the publisher of that software =)).
I'm using Windows 7 Ultimate 64 bit. The original file (which I chose to embed) is a 32-bit file. I have also tried with a dll file and the extracted dll file also couldn't work as the original one.
Could you please point out what's wrong in my code for extracting the resource or the file is corrupted right after I embed it as resource???
Your help would be highly appreciated. Thanks.