查找VSTO Outlook加载项的安装目录和工作目录;或者任何办公室,艾丁

查找VSTO Outlook加载项的安装目录和工作目录;或者任何办公室,艾丁,outlook,vsto,clickonce,working-directory,Outlook,Vsto,Clickonce,Working Directory,我创建了一个VSTO Outlook加载项,它使用一个库Html2Xhtml.dll(.NET),该库通过执行System.Diagnostic.Process.Start()调用另一个Html2Xhtml.exe 但是,它无法调用Html2xhtml.exe(我想),因为即使从Visual Studio启动,工作目录也是当前用户My Documents文件夹。我无法控制Html2Xhtml.dll中的代码,因此无法使用绝对路径;但是我想我可以在运行时更改外接程序的工作目录 但是,如果我通过Cl

我创建了一个VSTO Outlook加载项,它使用一个库Html2Xhtml.dll(.NET),该库通过执行System.Diagnostic.Process.Start()调用另一个Html2Xhtml.exe

但是,它无法调用Html2xhtml.exe(我想),因为即使从Visual Studio启动,工作目录也是当前用户My Documents文件夹。我无法控制Html2Xhtml.dll中的代码,因此无法使用绝对路径;但是我想我可以在运行时更改外接程序的工作目录


但是,如果我通过ClickOnce或其他方式安装,而我不知道用户将选择的安装路径,那么我应该如何找到我的Html2xhtml.exe

这是一个我不得不与之斗争相当长一段时间的真正问题。我必须使用的插件中使用的解决方案是将install dir写入注册表并从中读取值。这样,就可以找到无法嵌入exe的东西。这不是一个好的解决方案,但它奏效了

微软为什么坚持将DLL复制到随机目录的愚蠢“安全机制”,这是一个他们可能永远不会透露的秘密


在写我的评论时,我实际上有一个想法,到目前为止我还没有尝试过:让你的安装程序将你以后需要的文件复制到%appdir%\YourCompany\YourApplication\libs或类似的地方。你应该能够在运行时找到你的东西。

我遇到了一个类似的问题,并用Christoph描述的相同方法解决了它,我也想知道是否有其他方法可以做到这一点,但如果你没有找到任何方法,这里有一个例子

1) 使用以下InstallerClass创建自定义操作库

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Microsoft.VisualStudio.Tools.Applications;
using Microsoft.Win32;

namespace Setup.CustomActions
{
    [RunInstaller(true)]
    public partial class AddCustomization : Installer
    {
        static readonly Guid solutionID = new Guid("d6680661-c31e-4c24-9492-5919dc0uagt5");
        public override void Install(IDictionary stateSaver)
        {
            string installPath = Context.Parameters["installPath"];
            if(!String.IsNullOrEmpty(installPath))
            {
                AddTemplateToAvailableTemplates(installPath);
            }           
            base.Install(stateSaver);
        }

        public override void Rollback(IDictionary savedState)
        {
        }

        public override void Uninstall(IDictionary savedState)
        {
        }

        private void AddTemplateToAvailableTemplates(string installPath)
        {
            //The example below is very basic, put in checks to see whether the registry key already exists and so on
            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Office\14.0\Common", true);
            RegistryKey acturisKey = key.CreateSubKey(@"Spotlight\MyAppInstallPath");
            acturisKey.SetValue("InstallPath", installPath);h);
        }
    }
}
2) 在安装项目中,在安装自定义操作上创建一个指向安装目录的密钥:

如果您需要更多信息或想下载源代码,请查看Open Xml MVP Wouter Van Wugt发布的msdn文章,标题为“使用Windows Installer部署Visual Studio 2010 Tools for Office解决方案”

我找到了答案,全部归功于robindotnet.wordpress.com

//Get the assembly information
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());

ClickOnce应用程序也存在同样的问题。以下是获取加载项的部署路径所需的操作:

在应用程序中添加System.Deployment.Application引用

下一步是使用此属性检索部署路径:

ApplicationDeployment.CurrentDeployment.UpdateLocation.ToString()

就这样

对于COM插件,System.Reflection.Assembly.Location无法稳定地提供我们所需要的

但是,即使可以在注册表中保存安装目录,也不是必需的。因为: COM插件通常有一个ID。您可以使用GuidAttribute定义它。 在安装/注册插件期间,有关此程序集的信息存储在以下位置:

Computer\HKEY_CLASSES_ROOT\CLSID\{...myPlugin id ....}\InprocServer32
在属性“Codebase”中,您可以找到文件的路径

e.g.: file:///C:/Program Files/myPlugin.dll

这可能是最后的办法。。。看看有没有人能想出更好的主意。嗯,我试过各种各样的思考方法。事实是,程序集被复制到随机位置,因此找不到它带来的任何文件。如果你不想写入注册表,你可以。。。搜索?但这可能会让您搜索所有驱动器。谢谢deni,这对我很有帮助。=)这可能是有日期的,但这是很好的信息。请看我在下面找到的最新答案。