簡體   English   中英

如何在visual studio中修改setup項目的環境路徑

[英]How to modify the environment path for the setup project in visual studio

我在 Visual Studio 2010 中有一個安裝項目,用於創建安裝工具包 (MSI)。 我需要更新環境路徑以在安裝 MSI 時添加一個條目。 知道怎么做嗎?

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

謝謝托尼

我成功使用Visual Studio(2015)中的安裝項目並添加了一個自定義操作,改變了注冊表,如下面的答案所示:

PATH變量的GetEnvironmentVariable()和SetEnvironmentVariable()

以下代碼適用於應該應用於安裝項目的提交/安裝/卸載操作的自定義操作:

 [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);
        }
    }
}

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

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

您可以將其復制到.VBS文件中,並將該文件添加為安裝自定義操作。

我發現 ickydime 的回答非常有幫助,添加了執行程序集的路徑,這是完美的:

string pathUrl = System.IO.Path.GetDirectoryName(this.Context.Parameters["assemblypath"]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM