Set Visual Studio environment variables to debug a project

I am creating an extension for visual studio, and one of the requested functions is that it can change the environment variable to one of several parameters, which will then be inherited by the application, which is developed after its debugging.

I tried the following

Environment.SetEnvironmentVariable("foo", "bar", EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("foo", "bar", EnvironmentVariableTarget.Machine); 

but while this is being saved, the variable does not seem to pass it to the program after I clicked the run button.

I am looking for other ways to do this to try, and I don't mind if they are hacks.

Edit: for clarification, this process should be transparent to a debugged (arbitrary) program. It should also be a software solution.

+6
source share
3 answers

I have an assumption why the program you are debugging does not receive environment variables. The process reads environment variables when the process starts. And if you are developing a .NET application, Visual Studio creates the * .vshost.exe process to speed up debugging. Thus, Visual Studio does not create the start of a new process when starting debugging - so that the environment variables are not read.

Instead, you can use a memory mapped file to perform the required IPC .

+3
source

You can use compilation constants. Define the class that is responsible for getting your variables:

 public class MyEnvironment { public string SomeVariable{ get{ #if DEBUG return "bar"; #else return Environment.GetEnvironmentVariable("foo"); #endif } } } 

You can also use some kind of IOC to enter the instance of the variable provider. Either the production version that reads the environment, or the debug version with hard-coded values.

+4
source

I do not know if it is possible to change the settings in the program, but I would consider this question: How to set certain environment variables during debugging in Visual Studio?

If you start the process yourself, the StartInfo object, which is passed to Process.Start() , has an EnvironmentVariables property that you could also learn to use.

0
source

Source: https://habr.com/ru/post/955419/


All Articles