.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Saving an embedded file in C#

Posted By:Jean Paul       Posted Date: January 19, 2011    Points: 75    Category: C#    URL: http://www.dotnetspark.com  

Saving an embedded file in C#. This article provides solutions for saving image step by step

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:\windows\system32\regedt32.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

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend