2011-04-12 28 views
6

我是新來的Visual Basic,我有問題訪問我的項目的資源文件。如何在Visual Basic中獲取資源文件值?

Dim rm As Resources.ResourceManager = New Resources.ResourceManager("MyProjectName.My.Resources.Resources", [Assembly].GetExecutingAssembly()) 
Dim myValue = rm.GetString(lookUpKey) 'boom Object reference not set to an instance of an object. 

我認爲問題是與字符串「MyProjectName.My.Resources.Resources」。

將字符串移動到他們自己的資源文件中會更好嗎?

+0

是'lookUpKey'空? – manji 2011-04-12 18:02:10

+0

不是在我的項目資源文件中是「HD」字符串名稱。 *編輯將「值」更改爲「名稱」。 – 2011-04-12 18:04:47

+0

這些都不起作用。 – 2011-04-12 19:24:05

回答

1

我將無法訪問資源文件,直到我將.resx文件移入其自己的項目並從我的主項目中引用該項目。我還必須在該項目中創建一個虛擬類,以便將其編譯爲DLL文件。

訪問資源文件的代碼實際上位於生成的Resource.resx.vb文件中。

我能夠使用下面的代碼訪問資源文件。

'Name of Class Library where I moved the resx file 
Dim classLibraryName As String = "ResourceProj" 
'Name of Resource File without the .resx suffix 
Dim resourceFileName As String = "Mappings" 
'Finding the assembly of the resx file, ResourceProjClass is a dummy class I created so that the dll would build. 
Dim myAssembly As Assembly = GetType(ResourceProj.ResourceProjClass).Assembly 

Dim rm As Resources.ResourceManager = Nothing 
rm = New Resources.ResourceManager(classLibraryName & "." & resourceFileName, GetType(myAssembly) 
Return rm.GetString(lookUpKey) 
7

我認爲這是類似於:

my.Resource.whateverhere 

那是不是那種資源的您正在尋找?

+0

我在這2美分,我不得不使用我的資源的代碼是'Me.GetLocalResourceObject(「[ResourceKey]」)'。請注意,使用'GetLocalResourceObject'是因爲資源文件的文件夾是'Local_Resources'。 – Muffun 2016-06-07 16:02:13

1

嘗試ResourceManager("MyProjectName.Resources", ...),否則,如果它的應用程序資源,你可以簡單地使用My.Resources.HD(在這裏看到:My.Resources Object

打開反射器,在那裏加載你的程序集,轉到資源出現資源列表,搜索包含'HD'的資源,複製名稱(如MyProjectName.Resources.resources),刪除最後的.resources並嘗試使用該資源。

0

嘗試

Global.<MyNamespace>.My.Resources.<ResourceStringName> 

訪問資源字符串

0

簡單:

昏暗loginMessage的String = Global.Resources.NameOfYourResxFile.NameOFVariable

+1

請通過添加4個前導空格來格式化您的代碼。另外,代碼是做什麼的?我對VB沒有任何認識。 – SteveFest 2017-10-13 14:18:20