Adding Non .Net files as embedded resource

  • Thread starter Thread starter Jack Wright
  • Start date Start date
J

Jack Wright

Dear All,
I have added a Word document as an embedded resource in my assembly
that I try to extract and copy it into my client machine...but this is
not working...it gives junk characters...any ideas...

Stream outs = Assembly.GetExecutingAssembly().GetManifestResourceStream("ReadMe.doc");
outs.Seek(0, SeekOrigin.Begin);
StreamReader srException = new StreamReader(outs);
FileStream oFile = new FileStream("Test.doc", FileMode.CreateNew,
FileAccess.Write, FileShare.Write);
oFile.Close();
StreamWriter oWrite = new StreamWriter("Test.doc",
true,System.Text.Encoding.Unicode );
oWrite.Write(srException.ReadToEnd());
oWrite.Close();

Please help...

TALIA
Many Regards
Jack
 
Jack Wright said:
Dear All,
I have added a Word document as an embedded resource in my assembly
that I try to extract and copy it into my client machine...but this is
not working...it gives junk characters...any ideas...

Stream outs = Assembly.GetExecutingAssembly().GetManifestResourceStream("ReadMe.doc");
outs.Seek(0, SeekOrigin.Begin);
StreamReader srException = new StreamReader(outs);
FileStream oFile = new FileStream("Test.doc", FileMode.CreateNew,
FileAccess.Write, FileShare.Write);
oFile.Close();
StreamWriter oWrite = new StreamWriter("Test.doc",
true,System.Text.Encoding.Unicode );
oWrite.Write(srException.ReadToEnd());
oWrite.Close();

Word documents are binary files. You're using a StreamReader, which is
trying to convert it into text.

Use code such as:

using (Stream input = Assembly.GetExecutingAssembly().
GetManifestResourceStream("ReadMe.doc"))
{
using (Stream output = new FileStream("Test.doc",
FileMode.CreateNew,
FileAccess.Write,
FileShare.Write))
{
byte[] buffer = new byte[32768];
int read;

while ( (read=input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
}
}
 
* (e-mail address removed) (Jack Wright) scripsit:
I have added a Word document as an embedded resource in my assembly
that I try to extract and copy it into my client machine...but this is
not working...it gives junk characters...any ideas...

In addition to Jon's post: You can use the 'BinaryReader' to read the
'Stream' too.
 
Back
Top