如何获取资源文件中所有资源的名称

发布于 2024-07-25 06:16:39 字数 867 浏览 5 评论 0 原文

在 Visual Basic 项目中,我添加了一个包含一堆图像的资源文件 (resx)。

现在我想查询图像的名称。 如果我在 Visual Studio IDE 的设计器视图中打开 resx 文件并选择一个图像,属性网格会显示一个名称属性(默认为“没有扩展名的文件名,但可以更改)。

背景是我有一个图像列表,是在运行时创建的,并填充了资源文件中的图像,为了能够通过密钥访问这些图像,我必须将其设置

为如下(所有内容都是硬编码的):

Dim imagelist as new Imagelist
imageList.Images.Add("A", My.Resources.MyImages.A)
imageList.Images.Add("B", My.Resources.MyImages.B)
imageList.Images.Add("C", My.Resources.MyImages.C)
imageList.Images.Add("D", My.Resources.MyImages.D)
imageList.Images.Add("E", My.Resources.MyImages.E)
....
imageList.Images.Add("XYZ", My.Resources.MyImages.XYZ)

我想实现这一点

Dim imagelist as new ImageList

For Each img in GetMeAllImagesWithNameFromMyResourceFile
    imageList.Images.Add(img.Name, img.ImageFile)
Next

: Name 是一个字符串,ImageFile 是一个 System.Drawing.Bitmap

Within a Visual Basic Project I have added a resource file (resx) that contains a bunch of images.

Now I want to query the names of the images.
If I open the resx file in the designer view in the Visual Studio IDE and select an image, the property grid shows me a name property (defaults to "filename without extension but can be changed).

The background is that I have a imagelist that is created at runtime and populated with the images from the resource file. To be able to access these images by the key, I have to set it.

My code looks like this (everything hard coded):

Dim imagelist as new Imagelist
imageList.Images.Add("A", My.Resources.MyImages.A)
imageList.Images.Add("B", My.Resources.MyImages.B)
imageList.Images.Add("C", My.Resources.MyImages.C)
imageList.Images.Add("D", My.Resources.MyImages.D)
imageList.Images.Add("E", My.Resources.MyImages.E)
....
imageList.Images.Add("XYZ", My.Resources.MyImages.XYZ)

And I want to achive this:

Dim imagelist as new ImageList

For Each img in GetMeAllImagesWithNameFromMyResourceFile
    imageList.Images.Add(img.Name, img.ImageFile)
Next

where Name is a string and ImageFile a System.Drawing.Bitmap

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

牵你的手,一向走下去 2024-08-01 06:16:39

看看这段代码是否有帮助。

    Dim runTimeResourceSet As Object
    Dim dictEntry As DictionaryEntry

    runTimeResourceSet = My.Resources.ResourceManager.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, False, True)
    For Each dictEntry In runTimeResourceSet
        If (dictEntry.Value.GetType() Is GetType(Icon)) Then
            Console.WriteLine(dictEntry.Key)
        End If
    Next

我以 Icon 为例,如果您使用 Bitmap,则必须更改它。

编辑:您必须使用 dictEntry.Value & 的引用 看看如何使用它来将其添加到图像列表中。

See if this piece of code helps.

    Dim runTimeResourceSet As Object
    Dim dictEntry As DictionaryEntry

    runTimeResourceSet = My.Resources.ResourceManager.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, False, True)
    For Each dictEntry In runTimeResourceSet
        If (dictEntry.Value.GetType() Is GetType(Icon)) Then
            Console.WriteLine(dictEntry.Key)
        End If
    Next

I have used Icon as an example, which you will have to change if you are using Bitmap.

EDIT: You will have to use reference of dictEntry.Value & see how it can be used for adding it to imagelist.

故乡的云 2024-08-01 06:16:39

以下是用 C# 编写的,您应该能够轻松地将其转换为 VB。

Assembly executingAssembly = GetExecutingAssembly();

foreach (string resourceName in executingAssembly.GetManifestResourceNames())
{
    Console.WriteLine( resourceName );
}

现在您已经拥有了所有资源名称,您可以迭代列表并执行以下操作:

foreach(string s in executingAssembly.GetManifestResourceNames())
{
    if (s.EndsWith(".bmp"))
    {
        imgStream = a.GetManifestResourceStream(s);
        if (imgStream != null)
        {                    
            bmp = Bitmap.FromStream(imgStream) as Bitmap;
            imgStream.Close();
        }   
    }
}

我还没有尝试过此操作,但它应该可以工作。

The following is written in C#, you should be able to translate this to VB easily.

Assembly executingAssembly = GetExecutingAssembly();

foreach (string resourceName in executingAssembly.GetManifestResourceNames())
{
    Console.WriteLine( resourceName );
}

Now that you have all the resource names, you can iterate over the list and do something like:

foreach(string s in executingAssembly.GetManifestResourceNames())
{
    if (s.EndsWith(".bmp"))
    {
        imgStream = a.GetManifestResourceStream(s);
        if (imgStream != null)
        {                    
            bmp = Bitmap.FromStream(imgStream) as Bitmap;
            imgStream.Close();
        }   
    }
}

I have not tried this, but it should work.

神也荒唐 2024-08-01 06:16:39

尝试这样的操作:

Dim reader As New ResXResourceReader(resxFilePath)

Dim en As IDictionaryEnumerator
en = reader.GetEnumerator()

While en.MoveNext()
    Console.WriteLine("Resource Name: [{0}] = {1}", en.Key, en.Value)
End While

reader.Close()

您可以在 此链接。 这些示例是用 C# 编写的,但是针对 vb.net 修改它们并不难

Try something like this:

Dim reader As New ResXResourceReader(resxFilePath)

Dim en As IDictionaryEnumerator
en = reader.GetEnumerator()

While en.MoveNext()
    Console.WriteLine("Resource Name: [{0}] = {1}", en.Key, en.Value)
End While

reader.Close()

You can find more examples that could help you out at this link. The examples are written in C#, but it's not very hard to modify them for vb.net

痴意少年 2024-08-01 06:16:39

虽然上面的答案为我指明了正确的方向,但我添加了一个单独的答案来阐明 GetResourceSet 的使用以及随后的图像加载:

        Dim resSet As Resources.ResourceSet = My.Resources.ResourceManager.GetResourceSet(Globalization.CultureInfo.InvariantCulture, True, False)
        For Each de As DictionaryEntry In resSet
            If (de.Value.GetType() Is GetType(Bitmap)) Then
                m_Icons.Add(de.Key, My.Resources.ResourceManager.GetObject(de.Key))
            End If
        Next

请注意 My.Resources.ResourceManager.GetResourceSet 中的参数的以下内容:

  • 使用
    加载资源需要InvariantCulture
  • True ,因为此时在我的类库中我还没有访问资源集,这会强制加载它。 这似乎是 @bcrgen-steinblock 在他的评论中的意思,但在随后的编辑中被误解
  • false 对我来说没问题,因为我没有后备/默认资源集

While above answers pointed me in right direction, I am adding a separate answer to clarify use of GetResourceSet and subsequent loading of images:

        Dim resSet As Resources.ResourceSet = My.Resources.ResourceManager.GetResourceSet(Globalization.CultureInfo.InvariantCulture, True, False)
        For Each de As DictionaryEntry In resSet
            If (de.Value.GetType() Is GetType(Bitmap)) Then
                m_Icons.Add(de.Key, My.Resources.ResourceManager.GetObject(de.Key))
            End If
        Next

Note the following for arguments in My.Resources.ResourceManager.GetResourceSet:

  • use of
    InvariantCulture
  • True is required to load the resources, as at this point in my class library I have not yet accessed the resource set and this forces it to be loaded. This seems to be what @bcrgen-steinblock meant in his comment, but it was misunderstood in the ensuing edit
  • false is ok for me because I don't have a fallback / default resource set
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文