Suppose you are working on an application – which needs to extract another application on runtime. How to achieve the same?
We can start with the steps below:
Step 1: Create a new Console Application
Step 2: Embed an exe file inside it
For this – right click on the solution explorer window and choose “Add existing item”
In the appearing dialog box – type c:windowssystem32regedt32.exe
For convenience I am using the registry editor utility of windows.
(note: if your windows folder is in different drive you have to change the above path)
Now you can see the file added to solution explorer.
Choose the properties of file and change the Build Action property to Embedded Resource.
Now build your project – if successful you are ready with an exe embedded.
Step 3: Get the embedded resource name
Now our goal is to save the embedded exe in runtime to the same application folder. For achieving this we have to first get the embedded name of the exe file.
The embedded name of the resource file would be in the format ApplicationNameSpace.ResourceName.
In our case it is: EmbedExe.regedt32.exe
You can use the method
to get all the resource names in the assembly.
Step 4: Save the embedded resource
In this final step we are retrieving the bytes of assembly and saving into a file stream. For this we are using Stream, FileStream classes.
The code is given below:
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(“EmbedExe.regedt32.exe”);
FileStream fileStream = new FileStream(“new.exe”, FileMode.CreateNew);
for (int i = 0; i < stream.Length; i++)
We can see in the output folder that the new.exe is created