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

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

112771 次浏览

使用 ResXResourceReader 类

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());
}

你可以使用 资源管理器

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

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类的其他静态方法获得不同的程序集对象即可。希望对你有所帮助:)

添加资源的那一分钟。将 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 文件位于当前程序集或项目的范围内时,此方法才可用。否则,使用“脉冲”提供的方法。

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

  // 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 到 SQL:

XDocument
.Load(resxFileName)
.Descendants()
.Where(_ => _.Name == "data")
.Select(_ => $"{ _.Attributes().First(a => a.Name == "name").Value} - {_.Value}");

如果要使用 LINQ,请使用 resourceSet.OfType<DictionaryEntry>()。例如,使用 LINQ 可以根据资源的索引(int)而不是键(string)来选择资源:

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) ,ResourceSetResourceManager.GetResourceSet是不可用的。

使用 ResourceReader,正如本文建议的: “ C #-无法从 ResourceManager (从附属程序集)获取字符串

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

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);
}

我把我的 PDF 放在资源文件夹的 MVC5剃刀项目。我是这样打开文件的:

    public IActionResult ViewCCACH()
{
return GetDocumentLikeThis("FORM CC");
}


private IActionResult GetDocumentLikeThis(string likeThis)
{
ResourceManager MyResourceClass = new ResourceManager(typeof(Resources));


ResourceSet resourceSet = MyResourceClass.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
//ResourceManager resourceSet = new ResourceManager("ACH AUTH FORM CC Blank.pdf", System.Reflection.Assembly.Load("App_GlobalResources"));
foreach (DictionaryEntry entry in resourceSet)
{
string resourceKey = entry.Key.ToString();
object resource = entry.Value;
if (resourceKey.Contains(likeThis))
{
string RunningPath = AppDomain.CurrentDomain.BaseDirectory;
string FileName = string.Format("{0}Properties\\" + resourceKey + ".pdf", Path.GetFullPath(Path.Combine(RunningPath, @"..\..\..\")));


var fileStream2 = new FileStream(FileName, FileMode.Open, FileAccess.Read);
var fsResult2 = new FileStreamResult(fileStream2, "application/pdf");
return fsResult2;


}


}




return View();
}

Cshtml 中的代码很简单:

<a class="tablinks btn btn-primary m-2 pull-right" href="/Merchant/ViewCCACH" target="_blank">Download File</a>