.resxC# でファイル内のすべてのリソースをループする方法はありますか?
10 に答える
グローバリゼーションが考慮されるように、ファイルを直接読み取らず、常にリソース マネージャーを使用する必要があります。
using System.Collections;
using System.Globalization;
using System.Resources;
...
/* Reference to your resources class -- may be named differently in your case */
ResourceManager MyResourceClass =
    new ResourceManager(typeof(Resources));
ResourceSet resourceSet =
    MyResourceClass.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (DictionaryEntry entry in resourceSet)
{
    string resourceKey = entry.Key.ToString();
    object resource = entry.Value;
}
    私のブログでそれについてブログを書きました:)短いバージョンは、リソースの完全な名前を見つけることです(あなたがすでにそれらを知っていない限り):
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クラスの他の静的メソッドのいくつかを使用して別のアセンブリ オブジェクトを取得するだけです。それが役に立てば幸い :)
ResXResourceReader rsxr = new ResXResourceReader("your resource file path");
// 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());
}
      // 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();
リンクを参照してください:マイクロソフトの例
リソース .RESX ファイルをプロジェクトに追加するとすぐに、Visual Studio は同じ名前の Designer.cs を作成し、リソースのすべての項目を静的プロパティとして持つクラスを作成します。リソース ファイルの名前を入力した後にエディターでドットを入力すると、リソースのすべての名前が表示されます。
または、リフレクションを使用してこれらの名前をループすることもできます。
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" が提供するメソッドを使用します。
この方法の利点は、必要に応じてローカライズを考慮して、提供されている実際のプロパティを呼び出すことです。ただし、通常はリソースのプロパティを呼び出すタイプ セーフな直接メソッドを使用する必要があるため、かなり冗長です。
ResourceManager.GetResourceSetを使用できます。
LINQ を使用する場合は、 を使用しますresourceSet.OfType<DictionaryEntry>()。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);
}