Click here to Skip to main content
Click here to Skip to main content

An Extendable RESX - File Editor

, 22 Dec 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple RESX editor
Sample Image - TicoResourceWriter.jpg

Introduction

Editing RESX files in Visual Studio is not very convenient if you want to add content like image streams, icons and things like that. I work a lot with resource files because I don't like to add a form to a library just to be able to add an image stream as a resource.

I know that there are already pretty good open source RESX editors. But they have one thing in common that I don't like: If I want to add support for a new resource type, I have to change the code of the library. But what I want is a library that supports latebinding, so that I can provide my userdefined libraries with definitions of different resources.

The Library

The TicoResourceWriter library basically contains the following classes:

  • ResourceManager is a class that allows you to edit the Resource File. It's derived from System.Collections.CollectionBase. The Item type is IResourceHolder.
  • ClassLoader is a class that manages the several combinations of IResourceHolder and appropriate IResourceEditors. It's also derived from System.Collections.CollectionBase. It's self-managed so you should not try to add/remove items Smile | :) the Item type is ClassLoaderItem.
  • ClassLoaderItem is a class that manages one ResourceType. It allows you to create the IResourceHolder instance for the Resource you want to add, and the IResourceEditor instance to edit your Resource.
  • IResourceHolder is the Interface for any class that holds a Resource. It provides the Type of the Resource, the Name and the Resource itself.
  • IResourceEditor is the Interface for any class that provides editing of Resources.
  • DefaultResourceHolder is an abstract basic-Implementation of a Resource holder.
  • DefaultResourceEditor is an abstract basic-Implementation of a Resource editor.
  • ResourcePropertiesAttribute is an attribute that allows you to tell the ClassLoader which Resource kind you want to manage with which IResourceHolder, and what editor class you want to use.
  • There are also internal classes for the implementation of a StringHolder/Editor, IconHolder/Editor, ImageHolder/Editor and ImageStreamHolder/Editor.

Using the Library

To open a Resource file, just do the following:

ResourceManager mgr = new ResourceManager("C:\\blah.resx");
IResourceHolder itm = mgr.CreateHolderInstance(0);
mgr.Add(itm);
mgr.Edit(itm);
mgr.Save()

This creates an RESX file on c:\Blah.resx and adds one resource Item on it.

If you open a Resource File containing a Resource that has no appropriate IResourceHolder/Editor, ResourceManager will not let you save it. That's because the unknown Resources would be lost if you would save it.

So, if you want to have a complete Resource editor, you have to provide some classes by yourself. Just create a new ClassLibrary project and add TicoResourceWriter as reference and create two classes like this:

[ResourceProperties(typeof(fooEditor),"Foo Resource",typeof(fooType))]
public class fooHolder:DefaultResourceHolder
{
    public fooHolder()
    {
    }
    protected override string resourceTypeName
    {
        get
        {
            return "foo Resource";
        }
    }
    protected override object checkResource(object Value)
    {
        object RetVal = null;
        if (Value is fooType)
        {
            RetVal = base.checkResource(Value);
        }
        else
        {
            InvalidCastException up = new InvalidCastException("Value is not foo");
            throw up;
        }
        return RetVal;
    }
}
public class fooEditor:DefaultResourceEditor
{
    public fooEditor()
    {
    }
    protected override IResourceHolder checkTarget(IResourceHolder Value)
    {
        IResourceHolder RetVal = null;
        if (Value is fooHolder)
        {
            RetVal = base.checkTarget(Value);
        }
        else
        {
            InvalidCastException up = 
                    new InvalidCastException("Value is not a fooHolder");
            throw up;
        }
        return RetVal;
    }
    protected override void showEditor()
    {
        frmfooEditor tmp = new frmfooEditor();
        tmp.Value = Target.Resource as fooType;
        if (tmp.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            Target.Resource = tmp.Value;
        }
    }
}

Now, just add the frmfooEditor form to the project. Design it the way you want it and you have your new Resource Library.

If you want to use your library with the demo project, you create an XML file:

<?xml version="1.0" ?>
<Config>
<LoadLib>myFooLib.dll</LoadLib>
<LoadPath>myLibPath</LoadPath>
</Config> 

You have the choice to add only a single library by using LoadLib, or to add all DLLs in a certain directory by using LoadPath.

The XML file must be located in the directory where the executable lies and must be named REConfig.xml.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

m@u
Software Developer
Switzerland Switzerland
No Biography provided

Comments and Discussions

 
QuestionHow to add VERSION resource ? Pinmemberpedefetoll21-Jan-10 5:48 
GeneralGreat work! PinmemberP.Sandgren21-Sep-09 23:33 
GeneralExcelent work... PinmemberNarayanan Dayalan16-Feb-07 2:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 22 Dec 2006
Article Copyright 2006 by m@u
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid