示例代码:
Code
using System;
using System.Globalization;
using System.Threading;
using System.Resources;
using System.Reflection;
namespace ResourceManagerDemo
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
public static void Main()
{
Console.WriteLine(GetResource("Welcome","en-us"));
Console.WriteLine(GetResource("Welcome","zh-chs"));
}
public static string GetResource(string name, string culture)
{
// Create a resource manager to retrieve resources.
//Please notes that first parameter should be namespace+foldername+filename
ResourceManager rm = new ResourceManager("ResourceManagerDemo.Resources.Strings", 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.
string resource = rm.GetString(name,new CultureInfo(culture));
return resource;
}
}
}
using System;
using System.Globalization;
using System.Threading;
using System.Resources;
using System.Reflection;
namespace ResourceManagerDemo
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
public static void Main()
{
Console.WriteLine(GetResource("Welcome","en-us"));
Console.WriteLine(GetResource("Welcome","zh-chs"));
}
public static string GetResource(string name, string culture)
{
// Create a resource manager to retrieve resources.
//Please notes that first parameter should be namespace+foldername+filename
ResourceManager rm = new ResourceManager("ResourceManagerDemo.Resources.Strings", 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.
string resource = rm.GetString(name,new CultureInfo(culture));
return resource;
}
}
}
资源文件如下图: