有沒有辦法循環遍歷C#中.resx
文件中的所有資源?循環遍歷.resx文件中的所有資源
回答
您應該始終使用資源管理器,而不是直接讀取文件以確保全球化得到考慮。
using System.Collections;
using System.Globalization;
using System.Resources;
...
ResourceManager MyResourceClass = new ResourceManager(typeof(Resources /* Reference to your resources class -- may be named differently in your case */));
ResourceSet resourceSet = MyResourceClass.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (DictionaryEntry entry in resourceSet)
{
string resourceKey = entry.Key.ToString();
object resource = entry.Value;
}
我花了一點才弄明白,你需要這一行來聲明MyResourceClass。 'ResourceManager MyResourceClass = new ResourceManager(「Resources.ResourceFileName」,System.Reflection.Assembly.Load(「App_GlobalResources」));' – JoeFletch 2012-09-20 02:11:17
@JoeFletch:它不需要這一行。代碼直接調用資源文件。例如:我有一個名爲PageList.resx的文件,然後我會調用:ResourceSet resourceSet = PageList.ResourceManager.GetResourceSet(CultureInfo。CurrentUICulture,真實,真實); – Gabriel 2013-02-06 11:23:57
@Gabriel,感謝您的更新!我將不得不回到我的代碼來檢查這一點。 – JoeFletch 2013-02-06 13:25:07
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());
}
的博客上講述它on my blog :)短的版本是,尋找資源的全名(除非你已經知道他們):
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文件在當前程序集或工程的範圍內時纔可用。否則,使用「脈衝」提供的方法。
此方法的優點是您可以調用爲您提供的實際屬性,並根據您的需要考慮任何本地化。但是,它是相當多餘的,因爲通常你應該使用類型安全的直接方法來調用資源的屬性。
爲什麼在ResourceSet可用時使用反射? – 2010-01-11 10:12:18
這就是我想知道的(見最後一段)。只是想展示一種替代方法,但更重要的是,想要表明該課程是完全可以訪問的,並且不需要手工操作(第一段)。 – Abel 2010-01-11 10:14:02
// 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();
請注意,這個類位於'System.Windows.Forms'程序集中,如果您使用的是MVC應用程序,則不會自動添加 – 2016-08-22 17:18:06
使用LINQ to SQL:
XDocument
.Load(resxFileName)
.Descendants()
.Where(_ => _.Name == "data")
.Select(_ => $"{ _.Attributes().First(a => a.Name == "name").Value} - {_.Value}");
如果你想使用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);
}
- 1. 沒有循環遍歷所有文件
- 2. 循環遍歷ResourceManager中的所有資源 - C#
- 3. 循環遍歷R中的文件夾中的所有文件
- 4. 循環遍歷所有值
- 5. 循環遍歷maven中的特定資源文件以生成源文件
- 6. Shellscript循環遍歷文件夾中的所有文件
- 7. 循環遍歷Bash中的所有列
- 8. 循環遍歷UITableView中的所有UITextFields
- 9. 循環遍歷R中某個目錄中的所有文件
- 10. 通過Razor視圖中resx文件的資源循環
- 11. 循環遍歷文件
- 12. 循環遍歷文件名
- 13. 使用'for'循環遍歷目錄中的所有文件
- 14. 循環遍歷所有子文件夾並執行bat文件?
- 15. 循環遍歷所有具有特定擴展名的文件
- 16. 循環遍歷所有的記錄和
- 17. 循環遍歷類的所有對象
- 18. 循環遍歷matlab中的文件
- 19. 循環遍歷所有工作表VBA
- 20. 循環遍歷所有組合
- 21. 循環遍歷所有main()參數
- 22. 使用jQuery循環遍歷所有LI's
- 23. 如何循環遍歷所有路線?
- 24. 循環遍歷所有Unicode字符
- 25. Sqlite循環遍歷所有表
- 26. for循環不遍歷所有增量
- 27. For循環未遍歷所有數據
- 28. ASP.NET .resx資源文件中的有效資源密鑰
- 29. 顯示resx文件中的所有資源字符串
- 30. Powerpoint VBA循環沒有循環遍歷所有幻燈片
你能在RESX文件是否是內部項目還是你想闡述(或需要)讀取一個單獨的RESX文件或從另一個程序集讀取RESX? – Abel 2010-01-11 10:18:09