简体   繁体   English

在 VisualStudio C# 的调试 session 中将环境变量指定为命令行参数

[英]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%".我想在调试 session 中使用环境变量作为命令行参数。所以项目属性->调试->命令行 arguments:%TEMP% 没有给我临时路径作为参数,而是字符串“%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?!但我只想知道,如果一个 envvariable 起作用,什么时候起作用,它是如何起作用的?!

Does someone know how I can specify an envvar as a commandline parameter in VS debug commandline arguments?有人知道如何在 VS 调试命令行 arguments 中将 envvar 指定为命令行参数吗?

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. 该代码与该#if指令一起也仅在具有调试版本的情况下编译。 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. 如果程序是由Visual Studio启动的,则它将继承VS的环境变量。 VS gets its environment variables when it starts. VS在启动时会获取其环境变量。 So: 所以:

  1. Change/set the environment variable 更改/设置环境变量
  2. Restart Visual Studio 重新启动Visual Studio
  3. Launch/debug your program 启动/调试程序

You can use this feature and define few lauch enviroments in launchSettings.json file.您可以使用此功能并在launchSettings.json文件中定义一些启动环境。
It works both for asp.net web projects and console applications它适用于 asp.net web 项目和控制台应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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