2011-12-19 58 views
7

我創造我的程序
http://support.microsoft.com/kb/307353
一個Visual Studio 2008的安裝嚮導我看到它可以添加註冊表項等。 有無需編寫自定義操作註冊Windows環境變量的可能性?
如果我必須寫一個自定義操作,那麼最簡單的方法是什麼?是否可以在安裝嚮導項目中註冊環境變量?

+0

沒有Visual Studio 2009這樣的事情。我可以統計實例數量,一方面安裝程序實際需要註冊一個環境變量。很可能*非常好,你不是那種情況之一。 – 2011-12-19 10:53:04

+0

@CodyGray,請解釋可能的選擇。 – 2011-12-19 11:08:19

+1

我猜他會推薦用戶配置文件目錄/程序數據目錄中的註冊表或數據文件。或者如果你的項目是一個.Net項目,一個app.config值。雖然我不同意這個陳述的主旨 - 如果你將程序加入到PATH中,那麼這可能是一個很好的理由來解決環境變量問題。否則,我會同意可能有更好的地方放置您的數據。 – 2011-12-19 11:10:38

回答

6

使用Visual Studio 2008中,你可以很容易地在Windows註冊表中設置相應的變量做到這一點:

  1. 在解決方案資源管理器中,您的項目(不解決方案),單擊鼠標右鍵,並選擇查看 - >註冊表
  2. 創建註冊表項(文件夾):
    1. 對於用戶變量:在HKEY_CURRENT_USER單擊鼠標右鍵,選擇「New Key「,並將其命名爲」環境「。
    2. 對於系統變量:右鍵點擊HKEY_LOCAL_MACHINE,選擇 「新的密鑰」,並將其命名爲 「SYSTEM」。繼續執行此操作以創建路徑「HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Session Manager \ Environment」。在環境項(文件夾)
  3. 單擊鼠標右鍵,選擇新建 - >字符串,並給它你想要的名稱。
  4. 選定的字符串,找到屬性窗口(Alt + Enter鍵會彈出)
  5. 在屬性窗口中,填寫價值你想要的。

如果您希望值引用安裝目錄,您可以使用屬性變量來執行此操作:[TARGETDIR] SomeFile。EXT(見http://msdn.microsoft.com/en-us/library/aa370905%28v=vs.85%29.aspx更多的屬性變量)

1

Windows安裝程序不支持通過Environment表的環境變量,但Visual Studio安裝項目不允許您使用它。

一種解決方案是使用不同的設置的創作工具,它支持的環境變量:http://en.wikipedia.org/wiki/List_of_installation_software

另一種解決方案是通過用Orca編輯MSI手動添加它在環境表中。

還有您提到的自定義操作方法。

3

頂端回答解釋如何做沒有自定義操作,但那些尋找一個自定義操作可以使用下面的代碼作爲模板:

[RunInstaller(true)] 
public partial class GRInstallCustomAction : System.Configuration.Install.Installer 
{ 
    string environmentKey = @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment"; 
    string pathUrl = "C:\\Program Files (86)\\TargetFolder"; 
    public GRInstallCustomAction() 
    { 
     InitializeComponent(); 
    } 

    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)] 
    public override void Install(IDictionary stateSaver) 
    { 
     base.Install(stateSaver); 
    } 

    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)] 
    public override void Commit(IDictionary savedState) 
    { 
     base.Commit(savedState); 

     string environmentVar = Environment.GetEnvironmentVariable("PATH"); 


     //get non-expanded PATH environment variable    
     string oldPath = (string)Registry.LocalMachine.CreateSubKey(environmentKey).GetValue("Path", "", RegistryValueOptions.DoNotExpandEnvironmentNames); 


     var index = oldPath.IndexOf(pathUrl); 
     if (index < 0) 
     { 
      //set the path as an an expandable string 
      Registry.LocalMachine.CreateSubKey(environmentKey).SetValue("Path", oldPath + ";" + pathUrl, RegistryValueKind.ExpandString); 
     } 

    } 

    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)] 
    public override void Rollback(IDictionary savedState) 
    { 
     base.Rollback(savedState); 


    } 

    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)] 
    public override void Uninstall(IDictionary savedState) 
    { 
     base.Uninstall(savedState); 

     //get non-expanded PATH environment variable    
     string oldPath = (string)Registry.LocalMachine.CreateSubKey(environmentKey).GetValue("Path", "", RegistryValueOptions.DoNotExpandEnvironmentNames); 

     string removeString = pathUrl + ";"; 
     var index = oldPath.IndexOf(removeString); 
     if (index < 0) 
     { 
      removeString = pathUrl; 
      index = oldPath.IndexOf(removeString); 
     } 

     if (index > -1) 
     { 
      oldPath = oldPath.Remove(index, pathUrl.Length); 
      //set the path as an an expandable string 
      Registry.LocalMachine.CreateSubKey(environmentKey).SetValue("Path", oldPath, RegistryValueKind.ExpandString); 
     } 
    } 
} 

這一走通過向您展示如何創建和應用自定義操作: https://msdn.microsoft.com/en-us/library/d9k65z2d(v=vs.100).aspx

相關問題