【问题标题】:Resource manager exception, no solution seems to work资源管理器异常,似乎没有解决方案有效
【发布时间】:2017-01-09 20:26:53
【问题描述】:

尝试使用资源管理器从项目中的资源中获取字符串,我不断收到以下异常:

在 mscorlib.dll 中发生 system.Resources.MissingManifestResourceException' 类型的未处理异常。

所以我决定创建一个控制台应用程序来测试它,但我仍然遇到同样的问题,我尝试了各种解决方案,但总是遇到同样的异常。

这是我的代码:

using System;
using System.Reflection;
using System.Resources;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ResourceManager rm = new ResourceManager("Resource1", Assembly.GetExecutingAssembly());
            string someString = rm.GetString("test");

            Console.ReadKey();
        }
    }
}

【问题讨论】:

  • 阅读here,您应该可以通过:string someString = ConsoleApplication1.Resources.Resource1.test;访问资源
  • 不得不做一个小的编辑,由于某种原因,资源部分不会出现。但是谢谢你解决了我的问题!虽然我很想知道为什么其他解决方案不适合我

标签: c# embedded-resource resourcemanager


【解决方案1】:

你需要包含资源的命名空间,试试

ResourceManager rm = 
   new ResourceManager("ConsoleApplication1.Resource1", Assembly.GetExecutingAssembly());

我更喜欢使用这样的类型信息

 ResourceManager rm = new ResourceManager(typeof(ConsoleApplication1.Resource1));

这是关于使用 ResourceManager 的精彩 write-up

【讨论】:

  • 当时就是这样,感谢您提供的链接和解决方案,我计划在我的项目中充分利用资源管理器,这将是一个很大的帮助。
  • 很高兴它有帮助:)
【解决方案2】:

您必须完全限定资源名称:

ResourceManager manager = new ResourceManager("ConsoleApplication1.Resource1", Assembly.GetExecutingAssembly());

【讨论】:

    【解决方案3】:

    Quantic 提供的答案对我有用,谢谢 Quantic!

    string someString = ConsoleApplication1.Resources.Resource1.test;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-04
      • 2016-04-30
      • 1970-01-01
      • 1970-01-01
      • 2011-02-04
      • 1970-01-01
      • 2012-11-24
      相关资源
      最近更新 更多