-2

How to get windows environment variabe in the C# code.

  1. I have a windows startup script to set the environment variables.
  2. I need to pass this variable to C# code..something like %var%

e.g. in the C# file, replace D:\ with %var%. The value of var is stored as an environment variable in windows.

Thanks

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Shweta Chandrakar
  • 353
  • 2
  • 3
  • 12

2 Answers2

4

Use the System.Environment class.

The method:

System.Environment.GetEnvironmentVariable()

and

System.Environment.SetEnvironmentVariable()

Michael Freake
  • 1,197
  • 2
  • 14
  • 35
Gabe
  • 109
  • 4
0
myvalue =  System.Environment.GetEnvironmentVariable(nameofthedesiredVar);

This is the command.

icbytes
  • 1,831
  • 1
  • 17
  • 27