可以使用 ResourceManager 類在運行時檢索“嵌入的資源”(即已經編譯到應用程序或類庫中的資源)。ResourceManager 類的每個實例都與一個程序集關聯并且管理對嵌入到該程序集中的資源的檢索。
檢索資源
注意 嵌入的資源文件的基名稱是嵌入了資源文件的命名空間的名稱加上不帶任何擴展名的文件名。例如,myApplication 命名空間中名為 Resource1.resX 的文件的基名稱是 myApplication.Resource1。
demo1:
demo2:
using System; using System.Resources; using System.Threading; using System.Reflection; using System.Globalization; class ResourcesExample { public static void Main() { // Create a resource manager to retrieve resources. ResourceManager rm = new ResourceManager("resourceManager.Application", Assembly.GetExecutingAssembly()); // Get the culture of the currently executing thread. // The value of ci will determine the culture of // the resources that the resource manager retrieves. CultureInfo ci = Thread.CurrentThread.CurrentCulture; // Retrieve the value of the string resource named // "welcome", localized for the culture specified by ci. String str = rm.GetString("welcome", System.Globalization.CultureInfo.CurrentCulture); Console.WriteLine(str); } }
資源文件必須寫成,程序集名字.資源文件名不包括擴展名