Making a resource file programatically

My latest task was to make a set of image files into a single resource file to be used with a project. When the application loaded, it would pull the images into an ImageList for a Treeview. Well, this works fine except you now have 50 small files being distributed in a small folder with your application. The original design goal is to keep images separate while allowing them to be customizable for clients. So while trying to maintain customization, but making things simple and cleaner, I came upon the conclusion that I create a resource file for my icons.
First, I decided to make a tool to do all of this. I broke out trusty old VS 2008 and started a Windows Forms Application. To be honest, any chance I get to work on a Windows Form Application, I do. I miss the good ole days of desktop only applications – web apps suck to write and maintain – unless it is an email, blog, or social site type application. Data entry and business processing should have never been moved to the web – but companies will continue to spend 10x normal amount on a web app as they do on desktop app – I guess that’s progress!
Anyway, back to my utility. First thing, I slapped a button on the form and on the click of the button I kick of the directory browser. I navigate to where all my images are being stored and select that directory. I also placed a Textbox to enter my new resource file name. So, once I get a list of files to be imported I push the list into a Listbox and make a list of files (string[]).

private string[] FileList;
//Select Directory where my files are placed
private void btnBrowse_Click(object sender, EventArgs e)
{
FolderBrowserDialog dia = new FolderBrowserDialog();
if (dia.ShowDialog() == DialogResult.OK)
{
edDirectory.Text = dia.SelectedPath;
FileList = Directory.GetFiles(edDirectory.Text);
LoadFileList(edDirectory.Text);
}
}

//Load list box from the directory I selected.
private void LoadFileList(string path)
{
listBox1.Items.Clear();
foreach (string s in FileList)
{
string vStr = s.ToUpper();
string vPath = path.ToUpper() + "\\";
listBox1.Items.Add(vStr.Replace(vPath,""));
}
}

Once I have the file list and list box loaded, I invoked my method to generate the actual resource file.

public void GenerateResourceFile()
{
//I am putting my new file into the
string rPath = "C:\\Temp\\" + edFileName.Text + ".resx";
ResourceWriter rw = new ResourceWriter(rPath);
for (int idx = 0; idx < fname =" listBox1.Items[idx].ToString().ToUpper();" filepath =" FileList[idx].ToUpper();" image =" Image.FromFile(filePath);">

The reason you see me using listbox1 for file name and FileList for filepath is because I filtered out the paths in the listbox so I could have only the actual image file name as the “Key” in the resource file. The resource file I am creating is a KeyValuePair resource and can easily be loaded into a Dictionary for use in an application. Also, by creating this resource file and leaving it as part of a project and not embedding it in, this means we still maintain the ability to create custom icons for clients while keeping our install cleaner and not having a directory full of icons laying around on client machine.

Comments

Popular posts from this blog

It is on like Donkey Kong!

Open Letter to Microsoft

Writing a Video Game