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

Den*_*nis 3 .net c# vb.net office-interop

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

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

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 说:

=== 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.
Run Code Online (Sandbox Code Playgroud)

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

不知道这是否导致问题。

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

Eug*_*iev 9

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

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

在此处输入图片说明

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