2011-09-02 181 views
0

在VS 2010中,.Net 4.0
我試圖獲取給定dll中所有字符串資源的名稱
最終目標是,從一組文件中,能夠獲取所有資源,檢查哪些是字符串,並測試這些名稱以查看它們是否具有我正在尋找的關鍵詞。
重點是:我只有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文件的基本名稱。


我也嘗試與

myAssembly.GetManifestResourceNames() 

但它返回一個空數組

如果任何人有一個想法,我將不勝感激。

謝謝。

+0

在這裏檢查:http://stackoverflow.com/questions/27757/how-can-i-discover-the-path-of-an-embedded-resource你是100%確定你正在正確加載DLL並且它確實包含嵌入式資源? –

+0

我確信我已經嵌入資源(在resx文件中)。資源也設置爲PUBLIC而不是Friend。現在我不是100%確定我正確加載dll,但我認爲是這樣,因爲我可以執行此操作:例如,myAssembly.GetTypes。 – PatTheFrog

回答

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文件的內容(我們也應該將它們視爲字符串)。所以如果你不想要文件的文本,並且你期望字符串具有一定的長度,那麼可以進行簡單的測試。否則,我不知道如何解決這個問題。

相關問題