vb.net 从 .NET 3.5 更改为 4.5.1 时,无法加载文件或程序集“Interop.Microsoft.Office.Core,版本 = 2.4.0.0,...”

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

Could not load file or assembly 'Interop.Microsoft.Office.Core, Version=2.4.0.0, ...' when changing from .NET 3.5 to 4.5.1

c#.netvb.netoffice-interop

提问by Denis

I am upgrading my WinForms application from .NET 3.5 to 4.5.1.

我正在将我的 WinForms 应用程序从 .NET 3.5 升级到 4.5.1。

The 3.5 application works just fine. After the upgrade to .NET 4.5.1 I started getting the following exception:

3.5 应用程序运行良好。升级到 .NET 4.5.1 后,我开始收到以下异常:

System.IO.FileNotFoundException: Could not load file or assembly 'Interop.Microsoft.Office.Core, Version=2.4.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

File name: 'Interop.Microsoft.Office.Core, Version=2.4.0.0, Culture=neutral, PublicKeyToken=null'

System.IO.FileNotFoundException:无法加载文件或程序集“Interop.Microsoft.Office.Core,版本=2.4.0.0,Culture=neutral,PublicKeyToken=null”或其依赖项之一。该系统找不到指定的文件。

文件名:“Interop.Microsoft.Office.Core,版本=2.4.0.0,文化=中性,PublicKeyToken=null”

FusionLog says:

FusionLog 说:

=== Pre-bind state information ===
LOG: DisplayName = Interop.Microsoft.Office.Core, Version=2.4.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///C:/SampleApp/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : Interop.Microsoft.Office.Interop.Excel, Version=1.6.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\SampleApp\bin\Debug\SampleApp.vshost.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/SampleApp/bin/Debug/Interop.Microsoft.Office.Core.DLL.
LOG: Attempting download of new URL file:///C:/SampleApp/bin/Debug/Interop.Microsoft.Office.Core/Interop.Microsoft.Office.Core.DLL.
LOG: Attempting download of new URL file:///C:/SampleApp/bin/Debug/Interop.Microsoft.Office.Core.EXE.
LOG: Attempting download of new URL file:///C:/SampleApp/bin/Debug/Interop.Microsoft.Office.Core/Interop.Microsoft.Office.Core.EXE.

The strange part is that my references look the same except for 1 reference: in the .NET 3.5 project there was a reference to Microsoft.Office.Interop.Excelof Type COMand version 1.6.0.0(it was coming from the GAC) which I can't re-create on the .NET 4.5.1 project as there is no such thing in the COM Object References and I can't find this version in the .NET references.

奇怪的部分是,除了 1 个引用之外,我的引用看起来相同:在 .NET 3.5 项目中,我无法在 .NET 上重新创建Microsoft.Office.Interop.Excel类型COM和版本的引用1.6.0.0(它来自 GAC) 4.5.1 项目,因为 COM 对象引用中没有这样的东西,我在 .NET 引用中找不到这个版本。

Don't know if that is contributing to the problem.

不知道这是否导致问题。

Any ideas on how I can get this to work with .NET 4.5.1?

关于如何让它与 .NET 4.5.1 一起工作的任何想法?

回答by Eugene Astafiev

It doesn't matter what .Net framework version you run. It looks like you need to re-add the reference. Take a look at the COMtab in the Add Reference dialog of Visual Studio. The interop assembly will be generated for you automatically.

您运行的 .Net 框架版本无关紧要。看来您需要重新添加引用。查看Visual Studio 的“添加引用”对话框中的COM选项卡。互操作程序集将自动为您生成。

Pay special attention to the Embedd Interop Types property of the just added references:

特别注意刚刚添加的引用的 Embedd Interop Types 属性:

enter image description here

在此处输入图片说明

Anyway, the Could not load file or assembly Microsoft.Office.Interop.Excelforum thread describes exactly the same error.

无论如何,无法加载文件或程序集 Microsoft.Office.Interop.Excel论坛帖子描述了完全相同的错误。

回答by Nuno Ribeiro

I solved setting "Copy Local" and "Embed Interop Types" to true.

我解决了将“复制本地”和“嵌入互操作类型”设置为 true 的问题。

(I have Office 2013 and 365 installed)

(我安装了 Office 2013 和 365)

enter image description here

在此处输入图片说明