如何在Visual Basic中获取资源文件值?

Bif*_*iff 7 vb.net embedded-resource winforms

我是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.
Run Code Online (Sandbox Code Playgroud)

我认为问题在于字符串"MyProjectName.My.Resources.Resources".

将字符串移动到自己的资源文件中会更好吗?

Fre*_*nöw 8

我认为它类似于:

my.Resource.whateverhere
Run Code Online (Sandbox Code Playgroud)

这不是你想要的那种资源吗?


Bif*_*iff 0

在我将 .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)
Run Code Online (Sandbox Code Playgroud)