问题
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
回答1:
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.
回答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.
回答3:
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.
来源:https://stackoverflow.com/questions/2904053/how-to-add-application-folder-to-path-after-installation-vs-setup-project