Random programming things I'd want to remember

Showing posts with label embeddedresource. Show all posts
Showing posts with label embeddedresource. Show all posts

Wednesday, December 28, 2011

Get Stream for a BinaryReader for a file embedded in exe


public Stream GetDataFileStream(string resourceName)
  {
    foreach (string currentResource in System.Reflection.Assembly.
        GetExecutingAssembly().GetManifestResourceNames())
      if (currentResource.LastIndexOf(resourceName) != -1)
        {
          return System.Reflection.Assembly.GetExecutingAssembly().
            GetManifestResourceStream(currentResource);
        }

        throw new Exception("Resource not found : " + resourceName);
  }


//Then call this code:

using (BinaryReader reader = new BinaryReader(GetDataFileStream("data.dat")))
  {
    yourDataContainter = reader.ReadOrWhatever();
  }

In solution explorer, right-click on the text file that you want to embed, select "Properties". Under "Build Action", select "Embedded Resource", build solution.

How to extract embedded image/Как достать картинку из скомпилированного exe

Image _image;
List resourcenames = new List();//syntax highlighter glitch fix: 
resourcenames.AddRange(Assembly.GetExecutingAssembly().GetManifestResourceNames());
string resourcename = resourcenames.Find(
   delegate(string item) 
      { return item.EndsWith("template1.jpg"); });
Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourcename);
BinaryReader br = new BinaryReader(s);

MemoryStream ms = new MemoryStream(br.ReadBytes((int)s.Length));
_image = Image.FromStream(ms);
br.Close();
s.Close();
br.Dispose();
s.Dispose();

In solution explorer, right-click on the text file that you want to embed, select "Properties". Under "Build Action", select "Embedded Resource", build solution.

How to extract embedded text file/Как достать текстовый файл, скомпилированный в exe

        public static string ExtractResource(string resourceName)
        {
            foreach (string currentResource in System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames())
                if (currentResource.LastIndexOf(resourceName) != -1)
                {
                    string fqnTempFile = System.IO.Path.GetTempFileName();
                    string path = System.IO.Path.GetDirectoryName(fqnTempFile);
                    string rootName = System.IO.Path.GetFileNameWithoutExtension(fqnTempFile);
                    string destFile = path + @"\" + rootName + "." +
                    System.IO.Path.GetExtension(currentResource);

                    System.IO.Stream fs = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(currentResource);

                    byte[] buff = new byte[fs.Length];
                    fs.Read(buff, 0, (int)fs.Length);
                    fs.Close();

                    System.IO.FileStream destStream = new System.IO.FileStream(destFile,FileMode.Create);
                    destStream.Write(buff, 0, buff.Length);
                    destStream.Close();

                    return destFile;
                }

            throw new Exception("Resource not found : " + resourceName);

        }
In solution explorer, right-click on the text file that you want to embed, select "Properties". Under "Build Action", select "Embedded Resource", build solution.