2010-12-08 67 views
2

我在Visual Studio 2010中有一個安裝項目,用於創建安裝工具包(MSI)。 我需要更新環境路徑以在安裝MSI時添加條目。 任何想法如何做到這一點?如何在Visual Studio中修改安裝項目的環境路徑

我找不到讓我訪問環境的選項。唯一我看到的可能是直接編輯註冊表。還有什麼我能做的更好,或者那是我唯一的選擇?

感謝 託尼

回答

2

Visual Studio安裝項目不能設置環境變量。但是,您可以嘗試使用自定義操作。下面是一些示例VBScript代碼:

Set WshShell = CreateObject("WScript.Shell") 
Set WshEnv = WshShell.Environment("SYSTEM") 
WshEnv("Path") = WshEnv("Path") & ";myPath" 

可以在.vbs文件複製並添加文件作爲安裝自定義操作。

+0

我想,和它的作品, 感謝 – tony 2010-12-15 14:06:08

2

我有成功使用Visual Studio中的安裝項目(2015年),並補充說,改變註冊表,顯示在此答案自定義操作:

GetEnvironmentVariable() and SetEnvironmentVariable() for PATH Variable

下面的代碼是一個自定義的行動,應適用於安裝項目的提交/安裝/卸載和行動:

[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); 
     } 
    } 
} 
相關問題