Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在visual studio中修改安装项目的环境路径_C#_Windows Installer - Fatal编程技术网

C# 如何在visual studio中修改安装项目的环境路径

C# 如何在visual studio中修改安装项目的环境路径,c#,windows-installer,C#,Windows Installer,我在VisualStudio2010中有一个安装项目,用于创建安装工具包(MSI)。 我需要更新环境路径,以便在安装MSI时添加条目。 你知道怎么做吗 我找不到允许我访问环境的选项。我认为唯一可以做的就是直接编辑注册表。我能做得更好吗,还是那是我唯一的选择 谢谢 TonyVisual Studio安装项目无法设置环境变量。但是,您可以尝试使用自定义操作。以下是一些示例VBScript代码: Set WshShell = CreateObject("WScript.Shell") Set Wsh

我在VisualStudio2010中有一个安装项目,用于创建安装工具包(MSI)。 我需要更新环境路径,以便在安装MSI时添加条目。 你知道怎么做吗

我找不到允许我访问环境的选项。我认为唯一可以做的就是直接编辑注册表。我能做得更好吗,还是那是我唯一的选择

谢谢
Tony

Visual Studio安装项目无法设置环境变量。但是,您可以尝试使用自定义操作。以下是一些示例VBScript代码:

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

您可以将其复制到.VBS文件中,并将该文件添加为安装自定义操作。

我成功地在Visual Studio(2015)中使用了安装项目,并添加了更改注册表的自定义操作,如以下回答所示:

以下代码用于应应用于安装项目的提交/安装/和卸载操作的自定义操作:

 [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);
        }
    }
}
[运行安装程序(true)]
公共部分类GRInstallCustomAction:System.Configuration.Install.Installer
{
string environmentKey=@“SYSTEM\CurrentControlSet\Control\Session Manager\Environment”;
string pathUrl=“C:\\Program Files(86)\\TargetFolder”;
公共行动()
{
初始化组件();
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
公共覆盖无效安装(IDictionary stateSaver)
{
安装(stateSaver);
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
公共覆盖无效提交(IDictionary savedState)
{
提交(savedState);
字符串environmentVar=Environment.GetEnvironmentVariable(“PATH”);
//获取非扩展路径环境变量
string oldPath=(string)Registry.LocalMachine.CreateSubKey(environmentKey.GetValue(“Path”,“”,RegistryValueOptions.DoNotExpandEnvironmentNames);
var index=oldPath.IndexOf(路径URL);
如果(指数<0)
{
//将路径设置为可展开字符串
Registry.LocalMachine.CreateSubKey(environmentKey).SetValue(“Path”,oldPath+”;“+pathUrl,RegistryValueKind.ExpandString);
}
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
公共覆盖无效回滚(IDictionary savedState)
{
基本回滚(savedState);
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
公共覆盖无效卸载(IDictionary savedState)
{
卸载(savedState);
//获取非扩展路径环境变量
string oldPath=(string)Registry.LocalMachine.CreateSubKey(environmentKey.GetValue(“Path”,“”,RegistryValueOptions.DoNotExpandEnvironmentNames);
string removeString=pathUrl+“;”;
var index=oldPath.IndexOf(重新投资);
如果(指数<0)
{
removeString=pathUrl;
索引=oldPath.IndexOf(重新投资);
}
如果(索引>-1)
{
oldPath=oldPath.Remove(索引,路径URL.Length);
//将路径设置为可展开字符串
Registry.LocalMachine.CreateSubKey(environmentKey.SetValue(“Path”,oldPath,RegistryValueKind.ExpandString);
}
}
}

大家好,还有什么需要做的吗?我也试着做同样的事情,但它说脚本有问题。我检查了日志文件,它显示“自定义操作”\u 4E748D1D\u 9739\u 45AB\u A63B\u 527713367D4F脚本错误-2146827788,Microsoft VBScript运行时错误:变量未定义:“WScript'”是否使用完全相同的代码?请注意,WScript不能直接使用。您只能将其用作CreateObject参数。