【问题标题】: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从 .NET 3.5 更改为 4.5.1 时无法加载文件或程序集“Interop.Microsoft.Office.Core, Version=2.4.0.0, ...”
【发布时间】:2015-02-05 16:50:43
【问题描述】:

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

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

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

文件名:'Interop.Microsoft.Office.Core, Version=2.4.0.0, Culture=neutral, 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.

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

不知道这是否导致了问题。

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

【问题讨论】:

  • 你不能重新添加引用来更正它们吗?
  • @Mike:尝试过,重新添加了对“Microsoft Office 12.0 对象库”(COM,版本:2.4.0.0)的引用,但我找不到另一个引用。不用说没有帮助。似乎在运行时找不到它。它编译得很好。
  • office是什么版本的?
  • 只是为了验证,您是否选择了客户端配置文件或框架 4?
  • 我所做的只是在 VS2013 的“应用程序”选项卡中的“.NET Framework 3.5”和“.NET Framework 4.5.1”之间切换目标框架。不是客户资料。

标签: c# .net vb.net office-interop


【解决方案1】:

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

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

无论如何,Could not load file or assembly Microsoft.Office.Interop.Excel 论坛帖子描述了完全相同的错误。

【讨论】:

  • 我认为这不是真的,因为为什么将 .NET Framework 从 3.5 更改为 .NET 4.5.1 会导致问题发生,然后将 .NET Framework 更改回 3.5 会解决问题. (不涉及其他任何内容,只需更改“应用程序->目标框架”下拉菜单)。
  • 也许 COM 对象在 .NET 4.0 或更高版本中得到不同的处理?也许有些变得不兼容?
  • 我是否可能需要检查一些属性以在我的 bin/Debug 目录中生成/创建程序集?错误消息是正确的,它正在寻找的文件:“Interop.Microsoft.Office.Core.dll”不在目录中。需要做什么才能显示此文件?
  • 查看 PIA 的 Embedd Interop Types 属性。
【解决方案2】:

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

(我安装了 Office 2013 和 365)

【讨论】:

  • 我有 Office 2019。仅嵌入互操作类型是不够的,复制本地也必须设置为是
  • 我在使用不同的库时也遇到了类似的问题,但是打开嵌入互操作类型会给我一个错误:“无法嵌入互操作类型。请改用适用的接口。”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多