简体   繁体   中英

Specify environmental variables as commandline parameter in a debug session of VisualStudio C#

I want to use an environment variable as a commandline parameter in a debug session. So Project Properties->Debug->Command line arguments: %TEMP% gives me not the temp path as a parameter rather than the string "%TEMP%".

Of course I could resolve the environment variable to a path programmatically or copy past the correct path. But I want just to know, if an envvariable works and when, how it works?!

Does someone know how I can specify an envvar as a commandline parameter in VS debug commandline arguments?

You could modify your local environment variables for debugging in code. In your main entry point you can define as many enviroment variables as you like.

static void Main(string[] args) {
    #if DEBUG
    string path = Environment.GetEnvironmentVariable("path");
    Environment.SetEnvironmentVariable("path", path + @";c:\foo");
    Environment.SetEnvironmentVariable("temp", @"c:\bar");
    #endif

    new Program();
}

Together with that #if directive this code will also only compiled if you have a debug build. On release builds this code will be skiped (if you don't change your default settings of your project).

If the program is launched by visual studio then it inherits VS's environment variables. VS gets its environment variables when it starts. So:

  1. Change/set the environment variable
  2. Restart Visual Studio
  3. Launch/debug your program

You can use this feature and define few lauch enviroments in launchSettings.json file.
It works both for asp.net web projects and console applications

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM