Search code examples
.netvbcresgen

.NET: Resgen Tutorial


First off I have RFM and RFM and I have tried to follow a few sites but I cannot grasp the concept of the Resource Manager.

Can someone please explain to me how to generate a resource manager similar to that of the VS IDE.

For example if I compile with VBC from the commandline I can see all my resource files.

vbc /t:exe myfile.vb /res:res1 /res:res2

Dim a as Assembly = Assembly.GetExecutingAssembly()
For Each i as string in a.GetManifestResourceNames()
    Console.writeline(i)
Next i

res1

res2

If I compile with the VS IDE I only see:

myprogram.Resources.resource

How do I create a resource manager manually from the command line so that I can use the resource manager like I would with the VS IDE?

Dim CurrentResourceManager As New ResourceManager(_
"myprogram.Resources", CurrentAssembly)
Dim CurrentResourceSet As ResourceSet = CurrentResourceManager.GetResourceSet( _
CultureInfo.CurrentCulture, True, True)

When I do try to use resgen it complains about my file extention

C:...>resgen image.bmp myfile.Resource.resource source

ResGen : error RG0000: The file named "image.bmp" does not have a known extension. Managed resource files must end in .ResX, .txt, or .resources.

In the Manual it states to use RESXGEN for images, but I do not believe that is available in VS2008. I am stuck.

Update: Found this : http://msdn.microsoft.com/en-us/library/ekyft91f(VS.80).aspx Explains how to write a ResX file. Seems like a repeatable process. I am not sure why they would not include a utility with visual studio to create it.

I attempted to use the example of the ResourceWriter. Resgen puked upon using the MS Provided ResourceWriter Class.


Solution

  • Simplified version of a Visual Studios Resx generated file. I have removed Public Token and the Schema mappings. The Schema mapping is to assist you when creating/modifying these files. For demonstration and simplicity I have stripped it out.

    <?xml version="1.0" encoding="utf-8"?>
    <root>
      <resheader name="resmimetype">
        <value>text/microsoft-resx</value>
      </resheader>
      <resheader name="version">
        <value>2.0</value>
      </resheader>
      <resheader name="reader">
        <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral</value>
      </resheader>
      <resheader name="writer">
        <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral</value>
      </resheader>
      <assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=2.0.0.0, Culture=neutral" />
      <data name="mylibrary" type="System.Resources.ResXFileRef, System.Windows.Forms">
        <value>Resources\mylibrary.dll;System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral</value>
      </data>
      <data name="mytext" type="System.Resources.ResXFileRef, System.Windows.Forms">
        <value>Resources\mytext.txt;System.String, mscorlib, Version=2.0.0.0, Culture=neutral</value>
      </data>
    </root>
    

    For each type byte or string include the following data. Where name is the name you want to access it by and value is eitehr a relative or hard path:

    <data name="mytext" type="System.Resources.ResXFileRef, System.Windows.Forms">
        <value>Resources\mytext.txt;System.String, mscorlib, Version=2.0.0.0, Culture=neutral</value>
    <data>
    

    Compile your resource to a single resource manager file and include in your project

    resgen /compile myrex.resx
    
    vbc /t:exe myprogram.vb /res:myresx.resource
    csc /t:exe myprogram.cs /res:myresx.resource
    

    You have to create a resource manager in your program to access these files. If you want a strongly typed resource manager you must follow the Resource.Designer class from inside your project and modify that file. If you want a simple resource manager simply use GetString or GetBytes.

    Dim rm As New ResourceManager("rmc", [Assembly].GetExecutingAssembly())
    day = rm.GetString("mytext")
    o = rm.GetObject("mylibrary.dll")