2012-03-27 91 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() 

你去了!