Embedding files in assemblies, aka down with the ResourceManager.

I think the concept behind the ResourceManager is great, but I almost always find instances where it just isn't easy to get all of my resources formatted appropriately.  What I've been doing lately is simply packing my resources into the assembly as binary streams.  It seems this method I've been using for so long is actually unknown to most people, and they tend to find it quite useful.  I namely use it for bitmaps since they have a Stream constructor overload, but I also use it whenever I have a boilerplate config file, template, or anything else that I might need to set up the user experience without having to ship a bunch of files.  Here is a sample:

csc /t:exe /res:minimalConfig.config /res:defaultDocumentTemplate.template /res:superManUnderoos.bmp MyApplication.cs

The above results in a bunch of external files getting included right into my application.  Then I can set these up right at the beginning (normally this will be a Windows Forms application) of the Main method or in the Load event if I'm using Windows Forms.

using System;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

public class SimpleAppForm : Form {
    private Bitmap splashScreen;
    private string docTemplate;

    public SimpleAppForm() {
        this.Load += new EventHandler(this.Form_Load);
    }
   
    private void Form_Load(object sender, EventArgs e) {
        Assembly cur = Assembly.GetExecutingAssembly();

        // Loading a bitmap   
        splashScreen = new Bitmap(cur.GetManifestResourceStream("superManUnderoos.bmp"));
       
        // Loading a stringy template
        using(StreamReader sr = new StreamReader(cur.GetManifestResourceStream("defaultDocumentTemplate.template"))) {
            docTemplate = sr.ReadToEnd();
        }
       
        // Writing out a default configuration file
        if ( !File.Exists("settings.config") ) {
            using(StreamReader sr = new StreamReader(cur.GetManifestResourceStream("minimalConfig.config"))) {
                using(StreamWriter sw = new StreamWriter("settings.config")) {
                    sw.Write(sr.ReadToEnd());
                    sw.Close();
                }
                sr.Close();
            }
        }
    }
   
    [STAThread()]
    private static void Main(string[] args) {
        Application.Run(new SimpleAppForm());
    }
}

Remember to put the right types of security restrictions around the file writing if you plan on using that code-branch, but party as normal with the rest of the code since you are just pulling binary feeds out of your assembly and loading them up for use.  Hopefully this little tip will help all those users wondering what they need to do to get their apps shipping without an installer because they are single file copy deployable.

Published Wednesday, January 21, 2004 4:58 AM by Justin Rogers
Filed under: ,

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)