2

I am using .NET Core 6.0.100 and MS Visual Studio Community 2022 17.0.2

I develop a terminal app that uses target's MAC and IP addresses as an input. As it is meant to be an open source project (repo is not public yet) I'd like to avoid publishing my MAC address that I use while testing it, inside launchSettings.json while pushing to repo. I thought of using environment variable instead of hardcoding it.

Is it possible and sensible to refer to environment variable's value inside launchSettings.json without implementing additional logic inside a program or should I utilize .gitignore filters / some other solution?

Currently this is how my launchSettings.json looks (MAC Address shown is random of course):

{
  "profiles": { 
    "ProgramName": {
      "commandName": "Project",
      "commandLineArgs": "BC:34:2B:4C:AB:BE 192.168.0.12"
    }
  }
}

I tried passing command arguments as a value of environment variable "commandLineArgs": "$COMMAND_PARAMETERS" inside launchSettings.json but it is recognized as plain text instead of environment variable and so value: "$COMMAND_PARAMETERS" is passed as args[0].

Emil Kucharczyk
  • 124
  • 2
  • 12

1 Answers1

4

Yes it is possible to reference value of an environment variable and the solution turns out to be trivial. To do so simply enclose variable name with percent sign %.

Example launchSettings.json :

{
  "profiles": {
    "WakeFW": {
      "commandName": "Project",
      "commandLineArgs": "%COMMAND_PARAMETERS%"
    }
  }
}

EDIT:

Although the above solution works properly, I found it to be quiet troublesome when applied to debugging an app. During development I wanted to check app's behaviour with different input arguments so I changed environment variable's value and re-executed the app. It turned out to be using old variable's value, as if it was not updated.

Explanation: Environment variables are passed to MS Visual environment during its launch. MSV then creates child environment for debugged app, based on its own environment. If environment variable has been changed in the meantime, newly created environments would use its new value. However, those environments that have already been running would still use its old value instead. In order to "update" environment variable's value in MSV (which is a parent environment for debugged app) it is neccessary to restart it.

Related threads: Python: Environment Variables not updating Environment variable not updating with current value

Emil Kucharczyk
  • 124
  • 2
  • 12