Saving an embedded file in C#

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

image

Step 2: Embed an exe file inside it

For this – right click on the solution explorer window and choose “Add existing item”

image

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)

image

Now you can see the file added to solution explorer.

Choose the properties of file and change the Build Action property to Embedded Resource.

image

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

Assembly.GetExecutingAssembly().GetManifestResourceNames()

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++)
                fileStream.WriteByte((byte)stream.ReadByte());
            fileStream.Close();

We can see in the output folder that the new.exe is created

image

 

6 thoughts on “Saving an embedded file in C#

  1. sateesh says:

    Amazing with detailed explanation

  2. Vali says:

    How to Edit an embedded text file????????

    1. Jean Paul says:

      Hello Vali,

      We cannot edit an embedded file. They are meant as static resources.
      But on the runtime you can extract it and edit. Additionally, you have to load the resx from file system there after.

      Regards,
      Jean Paul

      1. Vali says:

        thanx a lot

  3. Eddie says:

    Hi Jean,

    “But on the runtime you can extract it and edit. Additionally, you have to load the resx from file system there after.” –> how do you load the edited resx file back to the EXE?

    Thanks,

    1. Jean Paul says:

      Hello Eddie,

      The aim of the article was to extract the files from a single exe file.

      Now doing the resource is much more complicated as there are recompiling, file lock issues.
      You can refer the CSharpCodeProvider for the purpose:
      http://support.microsoft.com/kb/319292

      Regards,
      Jean Paul

Leave a Reply

Your email address will not be published. Required fields are marked *