简体   繁体   中英

How to find the name of the default resources file in C# and VB.Net?

I've created an attribute, similar to the ToolboxBitmapAttribute, that allows you to associate an Icon or Image to a class.

To associate an image, the resource file base name, a type (to find the assembly), and the name of the resource are passed to the attribute constructor. I then use the ResourceManager to get access to the resource:

Dim rm = New ResourceManager(ResourceFileBaseName, passedType.Assembly)
Dim obj = rm.GetObject(resourceName)

This is fine except that I want the option to pass just the resource name in the constructor, and omit the resource file base name. Then I would pick up the resource from the default resource file. When I say default resource file, I mean the one when you start Visual Studio, open the property pages and click the Resources tab.

The problem is, when developing in VB.Net, the base name is 'RootNamespace.Resources', and when developing in C#, the base name is 'Rootnamespace.Properties.Resources'.

Where can I programatically find the name of the default resource file?

Update

What I can do is get a list of all resource names:

t.Module.Assembly.GetManifestResourceNames

I can then look for a name that ends with Resources.resources and use that to build the resource file base name.

This, however, is inefficient, but I suspect it's the only way to do it.

You could try all resource files in the assembly and test whether the icon is available.

It is going to very difficult to detect the default resource file. For example, I always move the resource files for my C# project to the root namespace. Also, the user may have purposefully put the image in a different resource file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM