Extract embeded resource in C#

To create embedded resource, Right click on item and set BuildAction as Embeded Resource.

Now extract embedded resource use following code.

public static string ExtractEmbededResource(string resourceName)

 //look for the resource name
 resourceName = “.” + resourceName;
 foreach (string currentResource in Assembly.GetExecutingAssembly().GetManifestResourceNames())
 if (currentResource.LastIndexOf(resourceName) != -1)
 string fqnTempFile = System.IO.Path.GetTempFileName();
 string path = System.IO.Path.GetDirectoryName(fqnTempFile);
 string rootName = System.IO.Path.GetFileNameWithoutExtension(fqnTempFile);
 string destFile = path + @”\” + rootName + “.” + System.IO.Path.GetExtension(currentResource);

 System.IO.Stream fs = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(currentResource);
 byte[] buff = new byte[fs.Length];
 fs.Read(buff, 0, (int)fs.Length);

 System.IO.FileStream destStream = new System.IO.FileStream(destFile,FileMode.Create);
 destStream.Write(buff, 0, buff.Length);
 return destFile;
 return "";

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: