posts - 18, comments - 23, trackbacks - 1

My Links

News

This blog has moved to http://shailen.sukul.org

Article Categories

Archives

Post Categories

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

               }

      }   

   }

}

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted on Friday, November 24, 2006 1:57 PM | Filed Under [ Articles ]

Feedback

Gravatar

# re: Helper code to extract embedded resource from project into Assembly

thanks, thats just what i was looking for.
11/12/2007 4:40 AM | tony
Gravatar

# re: Helper code to extract embedded resource from project into Assembly

Brilliant, exactly what I needed!
10/9/2008 4:23 AM | kevin
Gravatar

# re: Helper code to extract embedded resource from project into Assembly

// 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);
}
}
}
8/24/2009 6:04 AM | Dan Phillips
Gravatar

# re: Helper code to extract embedded resource from project into Assembly

thank you very much, i found it here and did well.
4/23/2010 3:31 PM | Bảo Vũ
Gravatar

# or for a c++/cli version

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);
}
3/31/2011 5:35 AM | Jan
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: