.resx
C #에서 파일의 모든 리소스를 반복하는 방법이 있습니까?
답변
항상 자원 관리자를 사용해야하며 다국어 지원을 고려하기 위해 파일을 직접 읽어서는 안됩니다.
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();
링크 참조 : Microsoft 예제
답변
리소스 .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);
}
