Click here to Skip to main content
15,893,381 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.4K   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 NUnit.Framework;
using Vestris.ResourceLib;
using System.Runtime.InteropServices;

namespace Vestris.ResourceLibUnitTests
{
    [TestFixture]
    public class ResourceIdTests
    {
        [Test]
        public void TestIntResourceId()
        {
            // zero resource id
            ResourceId zeroRid = new ResourceId(IntPtr.Zero);
            Assert.AreEqual(IntPtr.Zero, zeroRid.Id);
            Assert.AreEqual("0", zeroRid.Name);
            // known resource types
            foreach (Kernel32.ResourceTypes resourceType in Enum.GetValues(typeof(Kernel32.ResourceTypes)))
            {
                ResourceId rid = new ResourceId((IntPtr)resourceType);
                Assert.AreEqual(new IntPtr((uint)resourceType), rid.Id);
                Assert.AreEqual(((uint)resourceType).ToString(), rid.Name);
            }
        }

        [Test]
        public void TestStringResourceId()
        {
            // empty resource id
            ResourceId emptyRid = new ResourceId(String.Empty);
            Assert.AreEqual(String.Empty, emptyRid.Name);
            Assert.AreEqual(String.Empty, Marshal.PtrToStringUni(emptyRid.Id));
            // known resource types
            foreach (Kernel32.ResourceTypes resourceType in Enum.GetValues(typeof(Kernel32.ResourceTypes)))
            {
                ResourceId rid = new ResourceId((IntPtr) resourceType);
                Assert.AreEqual(resourceType, rid.ResourceType);
                Assert.AreEqual(((uint) resourceType).ToString(), rid.Name);
            }
            // string resource types
            ResourceId stringRid = new ResourceId("CUSTOM");
            Assert.AreEqual("CUSTOM", stringRid.Name);
            Assert.AreEqual("CUSTOM", Marshal.PtrToStringUni(stringRid.Id));
        }

        [Test, ExpectedException(typeof(InvalidCastException))]
        public void TestResourceTypeInvalid()
        {
            ResourceId invalidRid = new ResourceId("CUSTOM");
            Console.WriteLine(invalidRid.ResourceType);
        }

        [Test]
        public void TestResourceTypeResourceId()
        {
            // known resource types
            foreach (Kernel32.ResourceTypes resourceType in Enum.GetValues(typeof(Kernel32.ResourceTypes)))
            {
                ResourceId rid = new ResourceId(resourceType);
                Assert.AreEqual(resourceType, rid.ResourceType);
                Assert.AreEqual(((uint) resourceType).ToString(), rid.Name);
                Assert.AreEqual((IntPtr) resourceType, rid.Id);
            }
        }
    }
}

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