Question

Is there a way to directly access a string in a .resx?

I have several different projects, and the .resx was created using a SharpDevelop template "Misc>>Empty resource file."

I've tried most of the methods online, such as:

... = new ResourceManager ("ProjectEtc.resxFileName", 
           Assembly.GetExecutingAssembly)

but I receive the same error:

Could not find any resources appropriate for the specified culture or 
neutral culture. Make sure "ProjectEtc.resxFileName.resources" was 
correctly embedded or linked into assembly "Project calling" at compile time...

no matter what I seem to do.

It would appear that I need to convert my .resx too a .resource file, which I would rather not do.

Am I missing out on some steps by using a #develop template?

Wait a second... is this even the best way to store constant strings in .NET?

Thanks, Dane

Was it helpful?

Solution

Check the .resx file has its Build action set to EmbeddedResource. SharpDevelop should do that by default when you create a new file using the Empty resource file template but it is worth checking.

Now you need to pass the root namespace of your project and the resx file name without the file extension to the ResourceManager constructor.

So if you create a new project in SharpDevelop called TestResource my root namespace will be TestResource. Then if you add a resource file called Foo.resx to your project. Then add a string named "MyString" then you can access the corresponding string value with the following code:

Dim resourceManager As New ResourceManager("TestResource.Foo", Assembly.GetExecutingAssembly)
Dim result = resourceManager.GetString("MyString")

When you compile your application the .resx file will be compiled into a .resources file and embedded into your project. If you use a tool such as ILSpy you can see this embedded resource and what it is called.

Using .resx files is typically the way you do multilingual applications in .NET so using them to store strings is OK.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top