Blog Stats
  • Posts - 17
  • Articles - 2
  • Comments - 16
  • Trackbacks - 1

 

Friday, November 24, 2006

Helper code to extract embedded resource from project into Assembly

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();

               }

      }   

   }

}

 

 

Copyright © Shailen Sukul