2012-03-27 15 views
5

我创建了一个使用库Html2Xhtml.dll(.NET)一VSTO的Outlook加载项,其通过执行System.Diagnostic.Process.Start调用另一个Html2xhtml.exe()。查找VSTO Outlook Addin的安装目录和工作目录;或任何Office加载项

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

然而,如果我通过的ClickOnce或其他方式安装这个,我不知道用户要选择安装路径,我怎么想找到我的Html2xhtml.exe?

回答

16

我找到了答案here,完整学分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()); 
1

这是一个真正的问题,我与战斗相当长的一段时间。 AddIn I中使用的解决方案必须将安装目录写入注册表并从中读取值。这样的事情带来了不能嵌入到exe文件可以找到。这不是一个好的解决方案,但它的工作。

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

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

+0

这可能是最后的手段...看看是否有人带有一个更好的主意一起。 – Jake 2012-03-27 10:51:22

+0

那么,我尝试了各种反射的东西。事实是,程序集被复制到一个随机的位置,因此找不到它带来的任何文件。如果你不想写入注册表,你可以...搜索?但这可能会让您搜索所有驱动器。 – 2012-03-27 12:26:35

3

我也曾有过类似的问题,解决它以同样的方式由克里斯托弗描述的,我也想知道是否有这样做的任何其他方法,但如果你没有找到任何东西在这里是一个例子

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)在安装项目创建安装自定义操作,它指向安装目录的关键: Install custom action

如果您需要更多的信息或想下载源码看看this msdn发布的Open Xml MVP Wouter Van Wugt标题为“使用Windows安装程序部署Office解决方案的Visual Studio 2010工具”

+0

谢谢deni,这对我很有帮助。 =) – Jake 2012-03-28 13:38:05

+0

这可能会过时,但它是很好的信息。请查看我发现的最新答案。 – Jake 2012-05-23 04:34:27

0

对ClickOnce应用程序有同样的问题。以下是你需要做的就是在插件的部署路径是什么:

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

接下来就是使用这个属性来检索部署路径:

ApplicationDeployment.CurrentDeployment.UpdateLocation.ToString() 

你去了!