在Visual Studio 2022的VB.NET中读取.msg文件时遇程序集加载错误
解决VB.NET读取.msg文件时的Office程序集加载错误
错误原因
你遇到的System.IO.FileNotFoundException是因为程序找不到指定版本(15.0.0.0)的Office程序集,通常是Office安装缺失、引用版本不匹配导致的。
解决步骤
1. 检查本地Office安装
- 确认已安装Microsoft Office 2013及以上版本(版本15.0对应Office 2013),且安装时包含Outlook组件。
- 如果安装的是更高版本(如Office 2016/365,对应版本16.0),需要调整项目引用适配当前版本。
2. 修正项目引用
- 打开Visual Studio 2022,右键项目 → 管理NuGet程序包,搜索并安装
Microsoft.Office.Interop.Outlook。 - 若之前手动添加过COM引用,先在项目的引用列表中移除所有Office/Outlook相关项,再通过NuGet重新安装,避免版本冲突。
3. 添加程序集绑定重定向(可选)
若安装的Office版本与错误中的15.0不一致,可在App.config中添加绑定重定向,让程序自动适配已安装版本:
<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="office" publicKeyToken="71e9bce111e9429c" culture="neutral" /> <!-- 把newVersion改为你实际安装的Office版本号,16.0对应Office 2016/365 --> <bindingRedirect oldVersion="0.0.0.0-16.0.0.0" newVersion="16.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime>
4. 不依赖Office的替代方案
如果不想依赖本地Office环境,可使用纯.NET库读取.msg文件:
- 通过NuGet安装
MsgReader库。 - 替换代码为:
Imports MsgReader.Outlook Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim filePath As String = "C:\Email\Source\Tet.msg" Using reader As New Storage(filePath) Dim subject As String = reader.Subject Dim body As String = reader.BodyText ' 后续处理逻辑 End Using End Sub End Class
内容的提问来源于stack exchange,提问作者Yahya
相关产品推荐
相关产品推荐

