Is it possible to register environment variables in a setup wizard project?

I am creating a Visual Studio 2008 installation wizard for my program
http://support.microsoft.com/kb/307353
I see that you can add registry entries, etc. Is it possible to register a Windows environment variable without writing a custom action?
And if I have to write a custom action, what is the least painful way to do this?

+8
source share
4 answers

Warning : Please do not use this approach . This is dangerous. Use the built-in MSI functions to update environment variables.

Example : direct writing to HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment - Path destroy everything that is already there. Very seriously.

Solution : MSI has an Environment table that allows you to reliably combine and update environment variables. Please note that this table is also complex enough to cause unexpected results. Please check well. Delete scripts especially.


Using Visual Studio 2008, you can easily do this by setting the appropriate variable in the Windows registry:

  1. In Solution Explorer, right-click your project (not solution) and select View-> Registry
  2. Create a registry key (folder):
    1. For a user variable: right-click HKEY_CURRENT_USER , select New Key , and name it Environment .
    2. For a system variable: right- click HKEY_LOCAL_MACHINE , select " New Key " and name it " SYSTEM ". Continue to do this to create the path " HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Session Manager \ Environment ".
  3. Right-click on the Environment key (folder), select New-> String and give it the desired name.
  4. With the selected line, find the properties window (Alt + Enter will call it)
  5. In the Properties window, enter the desired value .

If you want the value to refer to the installation directory, you can do it this way using the property variables: [TARGETDIR] SomeFile.ext (see http://msdn.microsoft.com/en-us/library/aa370905%28v = vs. 85% 29.aspx for more variable properties)

+7
source

Windows Installer supports environment variables through the Environment table, but Visual Studio installation projects do not allow it.

The solution is to use another configuration tool that supports environment variables: http://en.wikipedia.org/wiki/List_of_installation_software

Another solution is to manually add it to the environment table by editing MSI using Orca .

You also mentioned an individual approach to action.

+3
source

The top answer explains how to do this without a special action, but those looking for a custom action can use the following code as a template:

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

This brochure shows how to create and apply special actions: https://msdn.microsoft.com/en-us/library/d9k65z2d(v=vs.100).aspx

+3
source

An example of a custom Ickydime action (see Answers above) shows how to add an environment variable by extending the PATH env variable. Below I will share how to create a new Environment.SetEnvironmentVariable variable using Environment.SetEnvironmentVariable

  string environmentKey = "MY_VAR"; string pathUrl = "%ProgramFiles%\\..."; public Installer1() { 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); Environment.SetEnvironmentVariable(environmentKey, pathUrl, EnvironmentVariableTarget.Machine); } [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); Environment.SetEnvironmentVariable(environmentKey, null, EnvironmentVariableTarget.Machine); } 
0
source

All Articles