首頁 > 後端開發 > C++ > 如何存取和迭代 C# .resx 檔案中的資源?

如何存取和迭代 C# .resx 檔案中的資源?

Susan Sarandon
發布: 2024-12-28 02:34:09
原創
241 人瀏覽過

How Do I Access and Iterate Through Resources in C#'s .resx Files?

在C# 中從.resx 檔案存取資源

在C# 應用程式中使用在地化資源時,您可能會遇到需要存取單一資源的情況。資源或遍歷所有資源。 .resx 檔案作為這些本地化資源的儲存容器,並且有內建的方法來方便資源存取。

循環存取資源

循環存取所有資源.resx 檔案中的資源,使用資源管理器而不是直接讀取檔案本身至關重要。這確保了全球化的正確實施。操作方法如下:

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

// Reference to your resources class
ResourceManager MyResourceClass = new ResourceManager(typeof(Resources));

// Get the resource set for the current culture
ResourceSet resourceSet = MyResourceClass.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);

// Iterate through each resource key and value
foreach (DictionaryEntry entry in resourceSet)
{
    string resourceKey = entry.Key.ToString();
    object resource = entry.Value;
}
登入後複製

遵循此方法,您可以有效地迭代 .resx 檔案中的所有本地化資源,存取資源鍵和對應的值。

以上是如何存取和迭代 C# .resx 檔案中的資源?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板