4

I'm looking for a easy way to include application installation folder to a %PATH% environment variable after installation is complete.

Visual Studio 2005/2008/2010, Setup Project.

Thank you

  • The only way I see is to create custom class library and do some coding, but, possibly, there's some other way - like using setx command – Dmitry Guyvoronsky May 25 '10 at 12:06
  • Possible duplicate of [GetEnvironmentVariable() and SetEnvironmentVariable() for PATH Variable](http://stackoverflow.com/questions/7121846/getenvironmentvariable-and-setenvironmentvariable-for-path-variable) – ickydime Jun 22 '16 at 20:54

3 Answers3

2

This is an old question but still ranks high in google results.

The link in the accepted answer is now broken.

However, you can find a duplicate question (asked later) that still has accurate answers here: GetEnvironmentVariable() and SetEnvironmentVariable() for PATH Variable

I flagged this question as duplicate but until it is closed here is the following code that worked for me:

string keyName = @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment";
//get non-expanded PATH environment variable            
string oldPath = (string)Registry.LocalMachine.CreateSubKey(keyName).GetValue("Path", "", RegistryValueOptions.DoNotExpandEnvironmentNames);
//set the path as an an expandable string
Registry.LocalMachine.CreateSubKey(keyName).SetValue("Path", oldPath + ";%MYDIR%",    RegistryValueKind.ExpandString);

I replaced %MYDIR% with the application path.

In addition, you will need to make a custom action to house this code and place the code within the commit function.

Community
  • 1
  • 1
ickydime
  • 1,051
  • 10
  • 22
2

Sad, but it still seems that you are right that it is required to code a class for the custom action. The example implementation has vanished. See below for an alternative.

Christian
  • 1,017
  • 3
  • 14
  • 30
  • Thanks, I came to a similar solution for this problem. – Dmitry Guyvoronsky Mar 31 '11 at 09:36
  • Does your solution remove the path at uninstall time? It seems that the required information is not passed at uninstall time via the IDictionary. My unit test works, when I put the output from install to uninstall. But it fails using the real installer. – Christian Mar 31 '11 at 09:59
  • 2
    thanks for this solution. what is missing is to broadcast the environment change to the system http://devio.wordpress.com/2011/04/06/adding-application-directory-to-path-variable-in-visual-studio-setup-projects/ – devio Apr 05 '11 at 22:58
  • @devio: The environment change is stored permanently in the windows registry. The new PATH will be available after reboot. I do not know a way to update existing sessions with the new PATH. What else do you mean with "broadcast to the system". – Christian Apr 06 '11 at 09:33
  • broadcasting will notify all application of system changes. cmd's already running are unaffected, but new a new cmd will have the new PATH setting. see http://support.microsoft.com/kb/104011 – devio Apr 06 '11 at 09:53
  • Thank you for the additional information. It should be possible to add the mentioned code from your blog article to the custom action. Feel free to add the information to my answer or to a new and separate answer. – Christian Apr 06 '11 at 11:57
  • @ickydime: can you please edit the answer or provide the changes link as a comment. TiA – Christian Jun 24 '16 at 13:23
  • @Christian Unfortunately I can not. I am not sure where the page went. Nor what the page said. That said, I did find an alternative answer and posted that below. – ickydime Jun 24 '16 at 17:11
0

The primary issue is that Visual Studio setups don't support the Windows Installer Environment table that can do all this with PATH and other environment variables. The MSI's Environment table isn't that complex, so it's worth using an MSI editor (such as Orca) to learn how to use it, then automate the MSI update with a post build step with a script (such as WiRunSql.vbs in the Windows SDK) to automate the update.

Alternatively, learn enough WiX to create a merge module containing the environment variables your setup needs, and add it to your Visual Studio setup.

Either of these choices is better than writing runtime code that requires care not to destroy the environment variables as well as not working for user variables in an Everyone install.

PhilDW
  • 20,260
  • 1
  • 18
  • 28