visual-studioenvironment-variablessetup-projectadvanced-installer

How to keep installation path while install my application and remove it while uninstalling it


I'm using VisualStudio 2022. I have to create a setup project for my C# WPF application in .NET Framework 4.8. The thing is, I have to add the installation path as an environment variable while installing the application. Also remove it while uninstalling the application. I found command for add and remove environment variable. It is as below,

setx APPQ_Path "C:\Program Files\AppQ" /M

and

reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v APPQ_Path /f

Plan is to keep this commands as batch files and execute it after installation and after uninstalling completed. I got some indications as shown below Custom Actions

  1. Is it possible to execute a batch file using this way?
  2. Installation path may change by user. So, I have to pass it to the first command as an argument. How to do?
  3. This Environment variable setting need to run as admin while I tested with cmd window. How to do it from installation setup process?
  4. Is there any other way to achieve this requirement?

Solution

  • Is there any other way to achieve this requirement?

    Yes. And, this answer removed my first 3 questions. Also changed the plan to use environment variable. Now, it is creating a file with path in the %ALLUSERSPROFILE%\CompanyName\AppName as said in the Mofi's comment.

    Here is how I did this.

    1. Install 'Microsoft Visual studio installer' from 'Manage Extensions'.
    2. Added a project to the solution of type 'Setup Wizard'.
    3. Right click on the project and click on 'Custom Actions' from 'View' option. enter image description here
    4. Add custom action for Install and Uninstall. enter image description here
    5. Add value in the Properties window of custom action. enter image description here
    6. Added an installer class in Startup project.
    7. Use override methods of 'Install' and 'Uninstall' for writing the path into a file (I named it as 'AppData.txt') in a common user path. (I used Environment.SpecialFolder.CommonApplicationData) enter image description here
    8. Write logic to delete the 'AppData' file in uninstall method.

    Note: We can use other override methods like 'OnAfterInstall' also.