C# MissingManifestResourceException 是什么意思以及如何解决它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1327692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does MissingManifestResourceException mean and how to fix it?
提问by Timwi
The situation:
情况:
- I have a class library, called
RT.Servers
, containing a few resources (of typebyte[]
, but I don't think that's important) - The same class library contains a method which returns one of those resources
- I have a simple program (with a reference to that library) that only calls that single method
- 我有一个名为 的类库,
RT.Servers
其中包含一些资源(类型为byte[]
,但我认为这并不重要) - 同一个类库包含一个返回这些资源之一的方法
- 我有一个简单的程序(引用那个库),它只调用那个单一的方法
I get a MissingManifestResourceException
with the following message:
我收到MissingManifestResourceException
以下消息:
Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Servers.Resources.resources" was correctly embedded or linked into assembly "RT.Servers" at compile time, or that all the satellite assemblies required are loadable and fully signed.
找不到适合指定文化或中性文化的任何资源。确保在编译时将“Servers.Resources.resources”正确嵌入或链接到程序集“RT.Servers”中,或者确保所有所需的附属程序集都可加载并已完全签名。
I have never played around with cultures, or with assembly signing, so I don't know what's going on here. Also, this works in another project which uses the same library. Any ideas?
我从来没有玩过文化,也没有玩过大会签名,所以我不知道这里发生了什么。此外,这适用于使用相同库的另一个项目。有任何想法吗?
采纳答案by Timwi
All I needed to do to fix this problem was to right-click the Resources.resx
file in the Solution Explorer and click Run Custom Tool. This re-generates the auto-generated Resources.Designer.cs
file.
解决此问题所需要做的就是Resources.resx
在解决方案资源管理器中右键单击该文件,然后单击Run Custom Tool。这将重新生成自动生成的Resources.Designer.cs
文件。
If the .resx file was added to the project manually, the Custom Tool property of the file must be set to "ResXFileCodeGenerator".
如果 .resx 文件是手动添加到项目中的,则该文件的自定义工具属性必须设置为“ResXFileCodeGenerator”。
The problem is due to a mismatch of namespaces, which occurs if you change the "default namespace" of the assembly in the project settings. (I changed it from (previously) "Servers"
to (now) "RT.Servers"
.)
问题是由于命名空间不匹配,如果您在项目设置中更改程序集的“默认命名空间”,就会发生这种情况。(我把它从(以前)改成"Servers"
了(现在)"RT.Servers"
。)
In the auto-generated code in Resources.Designer.cs
, there is the following code:
在自动生成的代码中Resources.Designer.cs
,有如下代码:
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Servers.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
The literal string "Servers.Resources"
had to be changed to "RT.Servers.Resources"
. I did this manually, but running the custom tool would have equally well done it.
文字字符串"Servers.Resources"
必须更改为"RT.Servers.Resources"
. 我手动完成了此操作,但运行自定义工具同样可以做得很好。
回答by TheVillageIdiot
From the Microsoft support page:
This problem occurs if you use a localized resource that exists in a satellite assembly that you created by using a .resources file that has an inappropriate file name. This problem typically occurs if you manually create a satellite assembly.
如果您使用的本地化资源存在于您通过使用具有不适当文件名的 .resources 文件创建的附属程序集中,则会出现此问题。如果您手动创建附属程序集,通常会出现此问题。
To work around this problem, specify the file name of the .resources file when you run Resgen.exe. While you specify the file name of the .resources file, make sure that the file name starts with the namespace name of your application. For example, run the following command at the Microsoft Visual Studio .NET command prompt to create a .resources file that has the namespace name of your application at the beginning of the file name:
要变通解决此问题,请在运行 Resgen.exe 时指定 .resources 文件的文件名。在指定 .resources 文件的文件名时,请确保文件名以应用程序的命名空间名称开头。例如,在 Microsoft Visual Studio .NET 命令提示符处运行以下命令以创建一个 .resources 文件,该文件在文件名的开头具有应用程序的命名空间名称:
Resgen strings.CultureIdentifier.resx
MyApp.strings.CultureIdentifier.resources
回答by Mark Rushakoff
I just came across this problem today, and I found this Microsoft Help and Support pagethat actually did work around the problem.
我今天刚遇到这个问题,我发现这个 Microsoft 帮助和支持页面实际上解决了这个问题。
I had a couple delegates at the top of my file, in the global namespace, and all of a sudden I was getting a MissingManifestResourceException
when running the program, on this line:
我的文件顶部有几个代表,在全局命名空间中,突然间我MissingManifestResourceException
在运行程序时得到了一个,在这一行:
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
Then I moved the delegates into the namespace, got the same error. Finally I put the delegates in the only class in that file, and the error went away, but I didn't want the delegates in that class or namespace.
然后我将委托移动到命名空间中,得到了同样的错误。最后,我将委托放在该文件中唯一的类中,错误消失了,但我不想要该类或命名空间中的委托。
Then I came across that link above, which said
然后我遇到了上面的那个链接,上面写着
To resolve this problem, move all of the other class definitions so that they appear after the form's class definition.
要解决此问题,请移动所有其他类定义,以便它们出现在表单的类定义之后。
I put the delegates (which I would not consider "class definitions") at the bottom of that file, outside of the local namespace, and the program didn't get the MissingManifestResourceException
anymore. What an irritating error. But, that seems like a more robust solution than modifying the auto-generated code :)
我将委托(我不会考虑“类定义”)放在该文件的底部,在本地命名空间之外,并且程序不再获得该MissingManifestResourceException
委托。多么令人恼火的错误。但是,这似乎是比修改自动生成的代码更强大的解决方案:)
回答by BlaM
I had the same problem, but using the Run Custom Toolcommand as suggested by Timwidid not help in my case.
我遇到了同样的问题,但是按照Timwi 的建议使用Run Custom Tool命令对我的情况没有帮助。
However it lead me into the right direction, because I ended up in the Propertiesof the .resxfile. Here I noticed a difference to another .resxfile that caused no problems.
然而,它引导我走向正确的方向,因为我最终进入了.resx文件的属性。在这里,我注意到与另一个没有引起问题的.resx文件的不同。
In my case I had to change the property "Build Action" from "Resource" to "Embedded Resource".
就我而言,我不得不将属性“构建操作”从“资源”更改为“嵌入式资源”。
My best guess for the reason is, that I had the .resxin a library that was used from another application. My application did not have its own .resxfile, so it had to use the one from the library - which is only available when it's embedded in the library and not "stand alone".
我最好的猜测是,我在另一个应用程序使用的库中拥有.resx。我的应用程序没有自己的.resx文件,因此它必须使用库中的文件 - 只有当它嵌入到库中而不是“独立”时才可用。
回答by Motti
I've run into a similar issue and, although I know it isn't the cause the OP had, I'll post it here so that if someone else runs across this problem in the future an answer will be available.
我遇到了类似的问题,虽然我知道这不是 OP 的原因,但我会在此处发布,以便将来如果其他人遇到此问题,将提供答案。
If you add a class before the designer class you will get a MissingManifestResourceException
exception at runtime (no compile time error or warning) because
如果在设计器类之前添加一个类,您将MissingManifestResourceException
在运行时收到异常(没有编译时错误或警告),因为
Visual Studio requires that designers use the first class in the file.
Visual Studio 要求设计人员使用文件中的第一个类。
For (slightly) more information see this post.
有关(稍微)更多信息,请参阅此帖子。
回答by Winston
I was getting the MissingManifestResourceException error after I ported my project from VS2005 to VS2010. I didn't have any other classes defined in the file that contains my Form class. And I also had my resx Resource File Name set correctly. Didn't work.
在将我的项目从 VS2005 移植到 VS2010 后,我收到了 MissingManifestResourceException 错误。我没有在包含我的 Form 类的文件中定义任何其他类。而且我还正确设置了我的 resx 资源文件名。没用。
So I deleted the resx files and regenerated them. All good now.
所以我删除了 resx 文件并重新生成了它们。现在一切都很好。
回答by nietras
另请参阅:使用 MSBuild 构建后运行测试时出现 MissingManifestResourceException(.mresource 在清单中有路径)
I repeat the answer here just for completeness:
为了完整起见,我在这里重复答案:
It appears adding LogicalName to the project file fixes it:
似乎将 LogicalName 添加到项目文件中修复了它:
<LogicalName>$(RootNamespace).Properties.Resources.resources</LogicalName>
i.e. so the embedded resource entry in the project file looks like this:
即项目文件中嵌入的资源条目如下所示:
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<LogicalName>$(RootNamespace).Properties.Resources.resources</LogicalName>
</EmbeddedResource>
</ItemGroup>
This is detailed in: http://blogs.msdn.com/b/msbuild/archive/2007/10/19/manifest-resource-names-changed-for-resources-files.aspx
这在:http: //blogs.msdn.com/b/msbuild/archive/2007/10/19/manifest-resource-names-changed-for-resources-files.aspx 中有详细说明
Note that we are using a .resx file, but the bug still appears to occur.
请注意,我们使用的是 .resx 文件,但该错误似乎仍然存在。
Update: The problem with resources (incl. XAML) appears to be related to output paths and the use of forward or backward slashes as detailed in: Why does modifying project output directories cause: IOException was unhandled "Cannot locate resource 'app.xaml'."
更新:资源(包括 XAML)问题似乎与输出路径和使用正斜杠或反斜杠有关,详见: 为什么修改项目输出目录会导致:IOException 未处理“无法找到资源‘app.xaml’ .”
回答by Markive
Because I am pre-compiling my web application (using VS2012 publish feature). I was getting the error above. I tried all the suggestions, but weirdly changing 'Build Action' to 'Content' did the trick!
因为我正在预编译我的 Web 应用程序(使用 VS2012 发布功能)。我收到了上面的错误。我尝试了所有建议,但奇怪地将“构建操作”更改为“内容”就成功了!
回答by codingismylife
Not sure it will help people but this one worked for me :
不确定它会帮助人们,但这个对我有用:
So the issue I had was that I was getting the following message:
所以我遇到的问题是我收到以下消息:
Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "My.Resources.Resources.resources" was correctly embedded or linked into assembly "X" at compile time, or that all the satellite assemblies required are loadable and fully signed"
找不到适合指定文化或中性文化的任何资源。确保在编译时将“My.Resources.Resources.resources”正确嵌入或链接到程序集“X”中,或者确保所有所需的附属程序集都可加载并完全签名”
I was trying to get the resources that were embedded in my project from another class library.
我试图从另一个类库中获取嵌入在我的项目中的资源。
What I did to fix the problem was to set the Access Modifier in the tab Project->Properties->Resources from "Internal" (accessible only within the same class library) to "Public" (accessible from another class library)
我为解决这个问题所做的是将选项卡 Project->Properties->Resources 中的 Access Modifier 从“Internal”(仅可在同一类库中访问)设置为“Public”(可从另一个类库访问)
Then run and voilà, no more error for me...
然后运行,瞧,对我来说不再有错误......
回答by Starnuto di topo
When I run in a similar issue, in Vs 2012, it turned out that the "Custom Tool Namespace" property of the resx file was wrong (in my case, actually, it was unset, so the generated code yeld this exception at runtime). My final set of properties for the resx file was something like this:
当我在类似的问题中运行时,在 Vs 2012 中,结果是 resx 文件的“自定义工具命名空间”属性是错误的(在我的情况下,实际上,它未设置,因此生成的代码在运行时抛出此异常) . 我为 resx 文件设置的最后一组属性是这样的:
- Build action: Embedded Resource
- Copy to Output Directory: Do not copy
- Custom Tool: ResXFileCodeGenerator
- Custom Tool Namespace: My.Project.S.Proper.Namespace
- 构建操作:嵌入式资源
- 复制到输出目录:不复制
- 自定义工具:ResXFileCodeGenerator
- 自定义工具命名空间:My.Project.S.Proper.Namespace