VSTO Word加载项DLL加载错误

时间:2013-07-10 15:25:14

标签: visual-studio dll ms-word vsto add-in

我创建了一个单词加载项,它使用了我的一个DLL。当我在Visual Studio(调试或发布模式)中运行它时,应用程序正常工作,但是当我尝试自己启动Word时(并且加载项仍然存在)然后继续触发加载DLL,我得到一个DLL错误,如下图所示。我很安静地确定问题与x86 / x64兼容性问题无关,因为我已经为所有项目将平台目标设置为x86。 (用于测试的Word版本也是32位)。任何关于问题可能出现的想法都将非常感谢,提前感谢!

enter image description here

1 个答案:

答案 0 :(得分:3)

所以经过一天的谷歌搜索,我找到了解决方案。事实证明,Word(以及一般的Office程序)将您的dll移动到单独的临时目录中 - 在以下位置找到:

$User\AppData\Local\assembly\dl3

我的dll依赖于同一目录中的其他资源,但由于它被单独移动到此临时目录中,因此无法加载dll。为了解决这个问题,我使用以下命令从安装目录中手动加载了DLL:

System.Reflection.Assembly.LoadFile(string path)

您可以使用以下代码行获取安装目录:

System.Reflection.Assembly assemblyInfo = System.Reflection.Assembly.GetExecutingAssembly();

//Location is where the assembly is run from 
string assemblyLocation = assemblyInfo.Location;

//CodeBase is the location of the ClickOnce deployment files
Uri uriCodeBase = new Uri(assemblyInfo.CodeBase);
string ClickOnceLocation = Path.GetDirectoryName(uriCodeBase.LocalPath.ToString());