Geeks With Blogs
Shailen Sukul blog

The following is a handy piece of code for extracing an embedded stream of characters from your assembly and saving to a text file.

1. Set the resource to Embedded.

2. Copy the following piece of code:

public static void WriteResourceToFile(string resourceName, string fileName)

{

      using (Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))

      {

         if (s != null)

         {

               byte[] buffer = new byte[s.Length];

               char[] sb = new char[s.Length];

               s.Read(buffer, 0, (int)(s.Length));

               /* convert the byte into ASCII text */

               for (int i = 0; i <= buffer.Length - 1; i++)

               {

                  sb[i] = (char)buffer[i];

               }

               using (StreamWriter sw = new StreamWriter(fileName))

               {

                  sw.Write(sb);

                  sw.Flush();

               }

      }   

   }

}

Posted on Friday, November 24, 2006 1:57 PM Articles | Back to top


Comments on this post: Helper code to extract embedded resource from project into Assembly

# re: Helper code to extract embedded resource from project into Assembly
Requesting Gravatar...
thanks, thats just what i was looking for.
Left by tony on Nov 12, 2007 4:40 AM

# re: Helper code to extract embedded resource from project into Assembly
Requesting Gravatar...
Brilliant, exactly what I needed!
Left by kevin on Oct 09, 2008 4:23 AM

# re: Helper code to extract embedded resource from project into Assembly
Requesting Gravatar...
// Simpler version


public static void WriteResourceToFile(string resourceName, string filepath)
{
using (Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
byte[] buffer = new byte[s.Length];
s.Read(buffer, 0, buffer.Length);
using (var sw = new BinaryWriter(File.Open(filepath, FileMode.Create)))
{
sw.Write(buffer);
}
}
}
Left by Dan Phillips on Aug 24, 2009 6:04 AM

# re: Helper code to extract embedded resource from project into Assembly
Requesting Gravatar...
thank you very much, i found it here and did well.
Left by Bảo Vũ on Apr 23, 2010 3:31 PM

# or for a c++/cli version
Requesting Gravatar...
void WriteResourceToFile(String^ resourceName, String^ filepath)
{
Stream^ s = Assembly::GetExecutingAssembly()->GetManifestResourceStream(resourceName);
array<unsigned char>^ buffer = gcnew array<unsigned char>((int)s->Length);
s->Read(buffer, 0, buffer->Length);
BinaryWriter^ sw = gcnew BinaryWriter(File::Open(filepath, FileMode::Create));
sw->Write(buffer);
}
Left by Jan on Mar 31, 2011 5:35 AM

Your comment:
 (will show your gravatar)


Copyright © Shailen Sukul | Powered by: GeeksWithBlogs.net