Resource Embedding - help!

  • Thread starter Thread starter Julie
  • Start date Start date
J

Julie

Hi everyone,
I'm trying to embed a text file into my project's DLL as a resource (via a
resx file), but I can't seem to get it to work.

Does anybody know how or know a tutorial on how to add it and access it via
code?

Thanks!
 
Hi everyone,
I'm trying to embed a text file into my project's DLL as a resource (via a
resx file), but I can't seem to get it to work.

Does anybody know how or know a tutorial on how to add it and access it via
code?

Thanks!

1) Add text file to project
2) Set Build Action property of text file to Embedded Resource
3) get content it from code

string resourceName = "WindowsApplication2.TextFile1.txt";
//string resourceName = "yourNamespace.[relative to project path to
your file].yourFileName.yourFileExtension";

System.Reflection.Assembly currAss =
System.Reflection.Assembly.GetExecutingAssembly();
MemoryStream bufferStream = new MemoryStream();

using (Stream s = currAss.GetManifestResourceStream(resourceName))
{
int readed = 0;

byte[] buffer = new byte[10 * 1024];

while ((readed = s.Read(buffer, 0, buffer.Length)) > 0)
{
bufferStream.Write(buffer, 0, readed);
}
}

string fileContent = System.Text.ASCIIEncoding.ASCII.GetString
(bufferStream.ToArray());
 
Back
Top