views:

65

answers:

1

How do I get all th resources dynamically from my current assembly? I have tried two methods, GetManifestResourceNames and GetResourceSet with no success. I am comfortable with solutions in VB.net or C#.

First Method

This first method only returns an array of length 1 with this value "MyNameSpace.Resources.resource". The problem is that there are more than 1 resource in this file.

Dim ca As Assembly = Assembly.GetExecutingAssembly
Dim rn() As String = CurrentAssembly.GetManifestResourceNames()

Second Method

Dim ca As Assembly = Assembly.GetExecutingAssembly
Dim crm As New ResourceManager("", ca)
''//Dim CurrentResourceManager As New ResourceManager(_
         "MyNamespace.Resources.resources", CurrentAssembly)
''//Dim CurrentResourceManager As New ResourceManager( _
         "My.Resources", CurrentAssembly)
Dim rs As ResourceSet = CurrentResourceManager.GetResourceSet(CultureInfo.CurrentCulture, True, True)
Dim rs As ResourceSet = crm.GetResourceSet( _
              CultureInfo.CurrentCulture, True, True)

MissingManifestResourceException was unhandled

Could not find any resources appropriate for the specified culture or the neutral culture. Make sure ".resources" was correctly embedded or linked into assembly "MyProgram" at compile time, or that all the satellite assemblies required are loadable and fully signed.loadable and fully signed.

Solution (as per Hans Passant)

Copy the Namespace from the Resources.Designer.vb

<Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Advanced)>  _
        Friend ReadOnly Property ResourceManager() As Global.System.Resources.ResourceManager
            Get
                If Object.ReferenceEquals(resourceMan, Nothing) Then
                    Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("MyNamespace.Resources", GetType(Resources).Assembly)
                    resourceMan = temp
                End If
                Return resourceMan
            End Get
        End Property

and place it in the code

Dim CurrentResourceManager As New ResourceManager( _
         "MyNamespace.Resources", CurrentAssembly)
Dim rs As ResourceSet = CurrentResourceManager.GetResourceSet( _
         CultureInfo.CurrentCulture, True, True)
+3  A: 

Yes, there's more than 1 resource in MyNameSpace.Resources.resource. Which is the name you have to pass to ResourceManager, an empty string isn't going to work.

To see the kind of code you have to write, start a Windows Forms application and add a couple of resources to the Project + Properties, Resources tab. In the Solution Explorer, click the "Show All Files" icon. Open the My Project node, open the Resources.resx node and double-click the Resources.Designer.vb file. Note the code for the ResourceManager property.

Hans Passant
+1 on the hint of where to look for example code. Sometimes I don't even realize it exists, and I always find it extremely useful.
Merlyn Morgan-Graham
Question: How come when I compile with "vbc /t:exe myfile.vb /res:myresouce1 /res:myresource2" I can get all my resources with GetManifestResourceNames but when I use the IDE they are all hidden?
Shiftbit
Cancel that... Resgen will generate a resx file.
Shiftbit
The IDE combines them into a single .resource, produced by Resgen.exe. Which is the proper thing to do. Your command line is similar to using Project + Add Existing Item and selecting a file, then setting its Build Action to "Embedded Resource". Start another thread if you have more questions.
Hans Passant
Right: http://stackoverflow.com/questions/3645077/net-resgen-tutorial
Shiftbit