PatTheFrog
PatTheFrog

Reputation: 167

How to get the String Resources from a dll file?

In VS 2010, .Net 4.0
I am trying to get the names of all the string resources in a given dll
The ultimate goal is, from a set of files, to be able to get all the resources, check which ones are Strings, and test the names to see if they have the key words I am looking for.
The main point is: I have only the dll file, nothing else.
Let say my file name is

    Dim myDllFullFileName as string = "C:\Bob.dll"

First, I guess I should get the Reflection :

    Dim myAssembly As New Reflection.Assembly 
    myAssembly=Reflection.Assembly.LoadFrom(myDllFullFileName)

Then the problems begin.
If I was simply looking for the string resources in my current project I could do that:

   Dim myResourceSet As Resources.ResourceSet 
   myResourceSet = My.Resources.ResourceManager.GetResourceSet(Globalization.CultureInfo.CurrentCulture, True, True)
   For Each Dict As DictionaryEntry In myResourceSet.OfType(Of Object)()
            If TypeOf (Dict.Value) Is String Then
                If Dict.Key.ToString.Contains("myOwnKeyHere") Then
                    'work with dict.value'
                End If
            End If
    Next

This would work fine for my current project. And the results are exactly what I am looking for.
My problem is that I want to do it with the dll files.
I think I should be able to built a ResourSet from myAssembly. For that, I will need a ResourceManager.
To build a new ResourceManager from myAssembly, I should probably use this method:

Public Sub New(baseName As String, assembly As System.Reflection.Assembly)

And that's where I am lost: I don't manage to get the baseName from the dll file that I am using.


I also try to with

 myAssembly.GetManifestResourceNames()

But it is return an empty array

If anyone has an idea, I would greatly appreciate.

Thanks.

Upvotes: 1

Views: 3978

Answers (1)

PatTheFrog
PatTheFrog

Reputation: 167

OK, here is one answer: I want to get the Name and Value of all the Resource String that are in a .dll or a .resx or a .exe file.

    Dim myDllFullFileName As String = "C:\Bob.dll"
    'it will also work for .resx and .exe files'
    Dim myAssembly As Reflection.Assembly
    myAssembly = Reflection.Assembly.LoadFile(myDllFullFileName)
    Dim myBaseNames(-1) As String
    myBaseNames = myAssembly.GetManifestResourceNames
    Dim myResourcesBaseName As String = ""
    For i As Integer = 0 To myBaseNames.Length - 1
        If myBaseNames(i).Contains(".Resources.") Then
            myResourcesBaseName = myBaseNames(i)
            'in the case of a .exe file, you will get more than one file'
            'Example with an .exe file from a Windows Application having one form:'
            'WindowsApplication1.Form1.resources'
            'WindowsApplication1.Resources.resources'
            'you want the one containing ".Resources."'
        End If
    Next
    myResourcesBaseName = myResourcesBaseName.Substring(0, myResourcesBaseName.LastIndexOf("."))
    'then you cut the last part and keep "WindowsApplication1.Resources"'

    Dim myManager As System.Resources.ResourceManager
    myManager = New System.Resources.ResourceManager(myResourcesBaseName, myAssembly)

    Dim myResourceSet As Resources.ResourceSet = Nothing
    myResourceSet = myManager.GetResourceSet(Globalization.CultureInfo.CurrentCulture, True, True) 'the second true (tryparent, is vital)'
    Dim myResourceNames(-1) As String
    Dim myResourceValues(-1) As String
    If myResourceSet IsNot Nothing Then
        myResourceNames = GetStringRessources(myResourceSet) 'see below for this routine'
        ReDim myResourceValues(myResourceNames.Length - 1)
        For i As Integer = 0 To myResourceNames.Length - 1
            myResourceValues(i) = myResourceSet.GetString(myResourceNames(i))
        Next
    End If

Here is the routine GetRessources:

 Public Function GetStringRessources(valResourceSet As ResourceSet) As String()
    Dim myList(-1) As String
    For Each Dict As DictionaryEntry In valResourceSet.OfType(Of Object)()
        If TypeOf (Dict.Value) Is String Then
            ReDim Preserve myList(myList.Length)
            myList(myList.Length - 1) = Dict.Key.ToString
        End If
    Next
    Return myList
End Function

This enables me to store the name and the value of all the resources strings.

You need to be careful about the following things:
-the code above will also return the content of TEXT files (they must be considered as strings as well, I suppose). So if you don't want the text of the files, and you expect strings of a certain length, a simple test is possible. Otherwise, I am not sure how to solve the problem.

Upvotes: 1

Related Questions