Resource Embedding - help!

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!
 
K

Kosmo

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top