什么可能导致 Visual Studio/C# 错误 MSB3105:重复资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/313718/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 22:45:54  来源:igfitidea点击:

What could cause Visual Studio / C# error MSB3105: Duplicate resources

c#visual-studio

提问by Cees Meijer

While working on an existing project I suddenly got the following error when trying to compile the solution:

在处理现有项目时,我在尝试编译解决方案时突然出现以下错误:

error MSB3105: The item "[filename]" was specified more than once in the "Resources" parameter. Duplicate items are not supported by the "Resources" parameter.

错误 MSB3105:在“资源”参数中多次指定了项目“[文件名]”。“Resources”参数不支持重复项。

Now, as far as I'm aware, I did not make any change to the project that affects the resources. Also I have checked each and every file within the project, but there is no duplicate reference anywhere to this file. Now I already found some forum entries regarding this error:

现在,据我所知,我没有对影响资源的项目进行任何更改。我还检查了项目中的每个文件,但没有任何地方对该文件的重复引用。现在我已经找到了一些关于这个错误的论坛条目:

1) Open the .csproj file and remove the duplicate reference. [Tried this, but I cannot find any duplicates in it]

1) 打开 .csproj 文件并删除重复的引用。[试过这个,但我找不到任何重复的内容]

2) In a 'partial class' project, move everything to a single class. [ Could try this, but the project has been split up into partial classes since the start, and I do not want to change this just because of the error ]

2)在“部分班级”项目中,将所有内容移至一个班级。[可以试试这个,但是项目从一开始就分成了部分类,我不想仅仅因为错误而改变它]

So what else could cause this ?

那么还有什么可能导致这种情况?

采纳答案by Theresa

Did you try showing all files in the Solution Explorer? You could have a duplicate .rsx file somewhere in there.

您是否尝试在解决方案资源管理器中显示所有文件?您可能在某处有一个重复的 .rsx 文件。

回答by Cees Meijer

I found the answer in .NET forum postingby Roy Green, and Theresa was right after all, though I did not recognize it.
If you have your main form class split up into partial classes, the partial sections end up in the solution explorer as separate items. And if you double click on them they show up in the designer mode as a normal form. But if you (accidentally) drop a control on these forms, Visual Studio creates a new .resx file and a InitializeComponent routine for it. But since this form is actually just part of the Main Form class it leads to the 'duplicate resources' error. And there is no other solution but to remove the InitializeComponent routine and delete the .resx file by hand.

我在Roy Green发布.NET 论坛中找到了答案,Theresa 毕竟是对的,尽管我不认识它。
如果您将主表单类拆分为部分类,则部分部分将作为单独的项目出现在解决方案资源管理器中。如果您双击它们,它们会在设计器模式中显示为正常形式。但是,如果您(不小心)在这些窗体上放置了一个控件,Visual Studio 会为其创建一个新的 .resx 文件和一个 InitializeComponent 例程。但由于此表单实际上只是 Main Form 类的一部分,因此会导致“重复资源”错误。没有其他解决方案,只能删除 InitializeComponent 例程并手动删除 .resx 文件。

回答by Cees Meijer

Be sure that under yourForm.cs no duplicate resources are defined (.resx). If you renamed your Form, remove the old resource because the new one during compile will be created with the new name.

确保在 yourForm.cs 下没有定义重复的资源 (.resx)。如果您重命名了表单,请删除旧资源,因为在编译期间将使用新名称创建新资源。

回答by choeerusak

I just made the same mistake. Delete the mainform.designer.vb, then I restored it again from the recycle bin, and found this error message when compiling. I try to search on google and someone suggested to check on .vbproj. Did that and found a duplicate on some line.

我刚刚犯了同样的错误。把mainform.designer.vb删了,然后我从回收站里又恢复了,编译的时候发现这个错误提示。我尝试在 google 上搜索,有人建议检查 .vbproj。这样做并在某行上发现了重复项。

回答by RayHAz

I had this as well, in VB. There is the "real form" file frmMain, and then I had created new class files and modified them to be Partial Public Class frmMain. For example, I have an ImportFromExcel.vbPartial Class file (I didn't want to clutter up the frmMain.vbwith the rather complicated Excel import code.)

我也有这个,在VB中。有“真实形式”文件 frmMain,然后我创建了新的类文件并将它们修改为Partial Public Class frmMain。例如,我有一个ImportFromExcel.vb部分类文件(我不想用相当复杂的 Excel 导入代码弄乱frmMain.vb。)

Everything worked fine until I decided I wanted to use an OpenFileDialogin the Sub ImportFromExcel. I dragged the OFD from the toolbox over to the Designer view of the ImportFromExcelfile. (I have no idea why this view exists, if you can't do anything with it!)But at any rate... dragging the OFD to the Partial Class Designer created an ImportFromExcel.resxfile. The drag/drop operation also created an InitializeComponent sub in ImportFromExcel, which is redundant and shows an error -- easily corrected with a little editing.

一切正常,直到我决定要在 Sub ImportFromExcel 中使用OpenFileDialog。我将 OFD 从工具箱拖到ImportFromExcel文件的设计器视图中。 (我不知道为什么这个视图存在,如果你不能用它做任何事情!)但无论如何......将 OFD 拖到 Partial Class Designer 创建了一个ImportFromExcel.resx文件。拖放操作还在ImportFromExcel 中创建了一个 InitializeComponent 子,它是多余的并显示错误——只需稍加编辑即可轻松纠正。

Ultimately, I chose to not use the dragged resource, but localized the code in the ImportFromExcel.vbfile.

最终,我选择不使用拖动的资源,而是将ImportFromExcel.vb文件中的代码本地化。

All you really have to do is right-click the ImportFromExcel.resxfile, and choose Delete. Everything else seems to "fix itself", and it builds fine now.

您真正需要做的就是右键单击ImportFromExcel.resx文件,然后选择删除。其他一切似乎都在“自我修复”,现在它构建得很好。

回答by Xav987

In my case, this problem happened because a file had the same name but not the same case in the GIT repository. For example MyFile.csand myFile.cs.

就我而言,发生此问题是因为文件在 GIT 存储库中具有相同的名称但大小写不同。例如MyFile.csmyFile.cs

If you do a checkout on windows, one of the files is overwritten by the other (no message, no warning). So, it is compiling, and we don't notice anything. But if you try to compile on Linux (with .NET Core) both files are present, and there is this error at compile time.

如果您在 Windows 上结帐,其中一个文件会被另一个覆盖(没有消息,没有警告)。所以,它正在编译,我们什么也没注意到。但是,如果您尝试在 Linux(使用 .NET Core)上进行编译,则这两个文件都存在,并且在编译时会出现此错误。