Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 循环遍历.resx文件中的所有资源_C#_.net_Embedded Resource - Fatal编程技术网

C# 循环遍历.resx文件中的所有资源

C# 循环遍历.resx文件中的所有资源,c#,.net,embedded-resource,C#,.net,Embedded Resource,有没有一种方法可以循环使用C中.resx文件中的所有资源?使用 您可以使用。您应该始终使用资源管理器,而不是直接读取文件,以确保考虑到全球化 using System.Collections; using System.Globalization; using System.Resources; 关于它的博客:简短的版本是,找到那些你已经知道的没有资源的人的全名: var assembly = Assembly.GetExecutingAssembly(); foreach (var re

有没有一种方法可以循环使用C中.resx文件中的所有资源?

使用


您可以使用。

您应该始终使用资源管理器,而不是直接读取文件,以确保考虑到全球化

using System.Collections;
using System.Globalization;
using System.Resources;

关于它的博客:简短的版本是,找到那些你已经知道的没有资源的人的全名:

var assembly = Assembly.GetExecutingAssembly();

foreach (var resourceName in assembly.GetManifestResourceNames())
    System.Console.WriteLine(resourceName);
要将它们全部用于某些用途:

foreach (var resourceName in assembly.GetManifestResourceNames())
{
    using(var stream = assembly.GetManifestResourceStream(resourceName))
    {
        // Do something with stream
    }
}

要在其他程序集中使用资源,而不是在执行程序集中使用资源,只需通过使用assembly类的其他一些静态方法获得不同的assembly对象。希望有帮助:

在您向项目中添加resource.RESX文件的那一刻,Visual Studio将创建一个同名的Designer.cs,为您创建一个将资源的所有项作为静态属性的类。键入资源文件名后,在编辑器中键入点时,可以看到资源的所有名称

System.Reflection.Assembly resourceAssembly = System.Reflection.Assembly.Load(new System.Reflection.AssemblyName("YourAssemblyName"));
String[] manifests = resourceAssembly.GetManifestResourceNames(); 
using (ResourceReader reader = new ResourceReader(resourceAssembly.GetManifestResourceStream(manifests[0])))
{
   System.Collections.IDictionaryEnumerator dict = reader.GetEnumerator();
   while (dict.MoveNext())
   {
      String key = dict.Key as String;
      String value = dict.Value as String;
   }
}
或者,您可以使用反射来循环这些名称

Type resourceType = Type.GetType("AssemblyName.Resource1");
PropertyInfo[] resourceProps = resourceType.GetProperties(
    BindingFlags.NonPublic | 
    BindingFlags.Static | 
    BindingFlags.GetProperty);

foreach (PropertyInfo info in resourceProps)
{
    string name = info.Name;
    object value = info.GetValue(null, null);  // object can be an image, a string whatever
    // do something with name and value
}
显然,只有当RESX文件在当前程序集或项目的范围内时,此方法才可用。否则,请使用pulse提供的方法

此方法的优点是,您可以调用为您提供的实际属性,如果愿意,可以考虑任何本地化。但是,这是相当多余的,因为通常您应该使用类型安全的直接方法来调用资源的属性

  // Create a ResXResourceReader for the file items.resx.
  ResXResourceReader rsxr = new ResXResourceReader("items.resx");

  // Create an IDictionaryEnumerator to iterate through the resources.
  IDictionaryEnumerator id = rsxr.GetEnumerator();       

  // Iterate through the resources and display the contents to the console.
  foreach (DictionaryEntry d in rsxr) 
  {
Console.WriteLine(d.Key.ToString() + ":\t" + d.Value.ToString());
  }

 //Close the reader.
 rsxr.Close();
请参阅链接:

使用:


如果要使用LINQ,请使用resourceSet.OfType。例如,使用LINQ可以根据资源的索引int而不是键字符串来选择资源:

ResourceSet resourceSet = Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (var entry in resourceSet.OfType<DictionaryEntry>().Select((item, i) => new { Index = i, Key = item.Key, Value = item.Value }))
{
    Console.WriteLine(@"[{0}] {1}", entry.Index, entry.Key);
}
对于nuget软件包System.Resources.ResourceManager v4.3.0,ResourceSet和ResourceManager.GetResourceSet不可用

使用ResourceReader,正如本文所建议的:

仍然可以读取资源文件的键/值

System.Reflection.Assembly resourceAssembly = System.Reflection.Assembly.Load(new System.Reflection.AssemblyName("YourAssemblyName"));
String[] manifests = resourceAssembly.GetManifestResourceNames(); 
using (ResourceReader reader = new ResourceReader(resourceAssembly.GetManifestResourceStream(manifests[0])))
{
   System.Collections.IDictionaryEnumerator dict = reader.GetEnumerator();
   while (dict.MoveNext())
   {
      String key = dict.Key as String;
      String value = dict.Value as String;
   }
}

简单的读循环使用此代码

var resx = ResourcesName.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, false, false);

foreach (DictionaryEntry dictionaryEntry in resx)
{
    Console.WriteLine("Key: " + dictionaryEntry.Key);
    Console.WriteLine("Val: " + dictionaryEntry.Value);
}

当有可用的资源集时,为什么要使用反射呢?这就是我想知道的,请看最后一段。只是想展示另一种方法,但更重要的是,想要证明这个类是完全可访问的,在第一段中你不需要手工操作。你能详细说明一下这个RESX文件是你的项目的内部文件,还是你想要或者需要读取一个单独的RESX文件,或者从另一个程序集中读取RESX吗?我花了一点时间才弄明白你需要这一行声明MyResourceClass。ResourceManager MyResourceClass=新建ResourceManager资源.ResourceFileName,System.Reflection.Assembly.LoadApp\u GlobalResources@乔弗利奇:它不需要这条线。代码直接调用资源文件。示例:我有一个名为PageList.resx的文件,然后我将调用:ResourceSet ResourceSet=PageList.ResourceManager.GetResourceSetCultureInfo.CurrentUICulture,true,true@加布里埃尔,谢谢你的更新!我将不得不回到我的代码来检查这一点。JoeFletch的话实际上帮助了我。我在另一个C项目中有一个resx,因此他的行允许我调用该dll程序集并以这种方式加载资源。另外,当我试图在代码中包含PageList时,它为PageList.ResourceManager抛出了一个错误。。页面列表不包含ResourceManager的定义。最后,字符串resourceKey=entry.Key抛出了一个错误,我改为使用object resourceKey=entry.Key。根据您的资源中是否定义了区域性,您必须切换到CultureInfo.InvariantCulture。在这里,我使用的是库中的资源,而不是WinForms应用程序。您能列举不在名为“资源”的文件夹中的资源吗?我想在名为“images”的文件夹中枚举项目中的所有资源映像。很确定它不关心它位于何处?测试它?我在一个文件夹中有用于输入sql文件的文件,这非常有效。我添加了从流到字符串的转换,因此我可以读取该文件,并且没有任何问题。请注意,此类位于System.Windows.Forms程序集中,如果您使用的是MVC应用程序,则不会自动添加此类
System.Reflection.Assembly resourceAssembly = System.Reflection.Assembly.Load(new System.Reflection.AssemblyName("YourAssemblyName"));
String[] manifests = resourceAssembly.GetManifestResourceNames(); 
using (ResourceReader reader = new ResourceReader(resourceAssembly.GetManifestResourceStream(manifests[0])))
{
   System.Collections.IDictionaryEnumerator dict = reader.GetEnumerator();
   while (dict.MoveNext())
   {
      String key = dict.Key as String;
      String value = dict.Value as String;
   }
}
var resx = ResourcesName.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, false, false);

foreach (DictionaryEntry dictionaryEntry in resx)
{
    Console.WriteLine("Key: " + dictionaryEntry.Key);
    Console.WriteLine("Val: " + dictionaryEntry.Value);
}