Click here to Skip to main content
15,892,643 members
Articles / Desktop Programming / Win32

File Resource Management Library (.NET)

Rate me:
Please Sign up or sign in to vote.
4.82/5 (27 votes)
16 Sep 2009CPOL14 min read 134.3K   2.6K   113  
A .NET implementation of a file resource management, with complete support for VS_VERSIONINFO version resources.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Vestris.ResourceLib
{
    /// <summary>
    /// A resource Id.
    /// There're two types of resource Ids, reserved integer numbers (eg. RT_ICON) and custom string names (eg. "CUSTOM").
    /// </summary>
    public class ResourceId
    {
        private IntPtr _name = IntPtr.Zero;

        /// <summary>
        /// A resource identifier.
        /// </summary>
        /// <param name="value">A integer or string resource id.</param>
        public ResourceId(IntPtr value)
        {
            Id = value;
        }

        /// <summary>
        /// A resource identifier.
        /// </summary>
        /// <param name="value">A integer resource id.</param>
        public ResourceId(uint value)
        {
            Id = new IntPtr(value);
        }

        /// <summary>
        /// A well-known resource-type identifier.
        /// </summary>
        /// <param name="value">A well known resource type.</param>
        public ResourceId(Kernel32.ResourceTypes value)
        {
            Id = (IntPtr) value;
        }

        /// <summary>
        /// A custom resource identifier.
        /// </summary>
        /// <param name="value"></param>
        public ResourceId(string value)
        {
            Name = value;
        }

        /// <summary>
        /// Resource Id.
        /// </summary>
        /// <remarks>
        /// If the resource Id is a string, it will be copied.
        /// </remarks>
        public IntPtr Id
        {
            get
            {
                return _name;
            }
            set
            {
                _name = IsIntResource(value)
                    ? value
                    : Marshal.StringToHGlobalUni(Marshal.PtrToStringUni(value));
            }
        }

        /// <summary>
        /// An enumerated resource type for built-in resource types only.
        /// </summary>
        public Kernel32.ResourceTypes ResourceType
        {
            get
            {
                if (IsIntResource())
                    return (Kernel32.ResourceTypes) _name;

                throw new InvalidCastException(string.Format(
                    "Resource {0} is not of built-in type.", Name));
            }
            set
            {
                _name = (IntPtr) value;
            }
        }

        /// <summary>
        /// Returns true if the resource is an integer resource.
        /// </summary>
        public bool IsIntResource()
        {
            return IsIntResource(_name);
        }

        /// <summary>
        /// Returns true if the resource is an integer resource.
        /// </summary>
        /// <param name="value">Resource pointer.</param>
        internal static bool IsIntResource(IntPtr value)
        {
            return (uint) value <= UInt16.MaxValue;
        }

        /// <summary>
        /// Resource Id in a string format.
        /// </summary>
        public string Name
        {
            get
            {
                return IsIntResource()
                    ? _name.ToString()
                    : Marshal.PtrToStringUni(_name);
            }
            set
            {
                _name = Marshal.StringToHGlobalUni(value);
            }
        }

        /// <summary>
        /// String representation of the resource Id.
        /// </summary>
        /// <returns>Resource name.</returns>
        public override string ToString()
        {
            return Name;
        }

        /// <summary>
        /// Resource Id hash code. 
        /// Resource Ids of the same type have the same hash code.
        /// </summary>
        /// <returns>Resource Id.</returns>
        public override int GetHashCode()
        {
            return IsIntResource() 
                ? Id.ToInt32() 
                : Name.GetHashCode();
        }

        /// <summary>
        /// Compares two resource Ids by value.
        /// </summary>
        /// <param name="obj">Resource Id.</param>
        /// <returns>True if both resource Ids represent the same resource.</returns>
        public override bool Equals(object obj)
        {
            if (obj is ResourceId && obj == this)
                return true;

            if (obj is ResourceId && (obj as ResourceId).GetHashCode() == GetHashCode())
                return true;

            return false;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
dB.
Team Leader Application Security Inc., www.appsecinc.com
United States United States
Daniel Doubrovkine has been in software engineering for twelve years and is currently development manager at Application Security Inc. in New York City. He has been involved in many software ventures, including Xo3 and Vestris Inc, was a development lead at Microsoft Corp. in Redmond, and director of Engineering at Visible Path Corp. in New York City. Daniel also builds and runs a foodie website, http://www.foodcandy.com.

Comments and Discussions