如何从DLL文件中获取字符串资源?

时间:2011-09-02 18:49:13

标签: .net visual-studio-2010 resources embedded-resource

在VS 2010中,.Net 4.0
我试图获取给定dll中所有字符串资源的名称 最终目标是,从一组文件中,能够获取所有资源,检查哪些是字符串,并测试名称,看看他们是否有我正在寻找的关键词。
重点是:我只有dll文件,没有别的 假设我的文件名是

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

首先,我想我应该得到反思:

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

然后问题就开始了 如果我只是在当前项目中查找字符串资源,我可以这样做:

   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

这对我当前的项目很好。结果正是我要找的 我的问题是我想用dll文件来做 我想我应该可以从myAssembly构建一个ResourSet。为此,我需要一个ResourceManager 要从myAssembly构建一个新的ResourceManager,我应该使用这个方法:

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

这就是我迷失的地方:我无法从我正在使用的dll文件中获取baseName。


我也试着用

 myAssembly.GetManifestResourceNames()

但它返回一个空数组

如果有人有想法,我将非常感激。

感谢。

1 个答案:

答案 0 :(得分:1)

好的,这是一个答案: 我想获取.dll或.resx或.exe文件中所有资源字符串的名称和值。

    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

以下是例程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

这使我能够存储所有资源字符串的名称和值
你需要注意以下事项:
- 上面的代码也将返回TEXT文件的内容(我想它们也必须被视为字符串)。因此,如果您不想要文件的文本,并且您需要一定长度的字符串,则可以进行简单的测试。否则,我不知道如何解决问题。