"未能找到任何适合于指定的区域性或非特定区域性的资源"错误的解决方法

ContractedBlock.gif ExpandedBlockStart.gif 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);
    }
}

以上是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);
        }
    }
}
以上代码运行正常!

Google了半天发现几乎所有的解决建议都放在ResourceManager类枸造函数语法上,折腾了半天才搞明白,把解决方法放上来,希望有用。

转载于:https://www.cnblogs.com/mrwsr/archive/2009/08/26/1554031.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值