Code
using System;
using System.Globalization;
using System.Threading;
using System.Resources;
using System.Reflection;
class ResourcesExample
{
public static void Main()
{
// Create a resource manager to retrieve resources.
ResourceManager rm = new ResourceManager("items",
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", ci);
Console.WriteLine(str);
}
}
using System;
using System.Globalization;
using System.Threading;
using System.Resources;
using System.Reflection;
class ResourcesExample
{
public static void Main()
{
// Create a resource manager to retrieve resources.
ResourceManager rm = new ResourceManager("items",
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", ci);
Console.WriteLine(str);
}
}
以上是MSDN上ResourceManager类的一个范例,ResourceManager枸造函数的第一个参数格式应该为“namespace+resource文件名”,如要获取项目webProject1上r的资源文件Resource1.resx,则第一个参数应为“webProject1.Resource1"。
然而我却死活出现以下错误:
未能找到任何适合于指定的区域性或非特定区域性的资源。请确保在编译时已将“WebSiteCSharp.Resource.resources”正确嵌入或链接到程序集“App_Web_qlcb8kez”,或者确保所有需要的附属程序集都可加载并已进行了完全签名。
经过排查发现使用“网站”的方式(而不是“项目”)在编译时会生成随机文件名的程序集(在以上错误信息中为“App_Web_qlcb8kez”),即是项目的namespace,每次编译后都不一样,所以在使用namespace作为ResourceManager枸造函数的参数时会导致运行时错误。
而在使用“项目”的方式时,因为namespace是固定不变的,所以不会出现以上问题:
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Resources;
using System.Globalization;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
ResourceManager rm = new ResourceManager( " WebApplication1.Resource " , System.Reflection.Assembly.GetExecutingAssembly());
String myName = rm.GetString( " MyName " );
Response.Write(myName);
}
}
}
以上代码运行正常!
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Resources;
using System.Globalization;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
ResourceManager rm = new ResourceManager( " WebApplication1.Resource " , System.Reflection.Assembly.GetExecutingAssembly());
String myName = rm.GetString( " MyName " );
Response.Write(myName);
}
}
}
Google了半天发现几乎所有的解决建议都放在ResourceManager类枸造函数语法上,折腾了半天才搞明白,把解决方法放上来,希望有用。