Lokesh
Lokesh

Reputation: 849

Trying to retrieve the environment variable returns null

I have set a variable to the user with the below command.

c:\>set something="C:\programfiles\win\something"

When I try to get the variable using System.Environment.GetVariable("something"), I get the value as null.

Please can anyone explain the problem on this?

Upvotes: 2

Views: 3221

Answers (2)

svick
svick

Reputation: 244757

If you set environment variable using the set command, that variable is actually set only in the current console. So, if you have application that prints out the variable something called var.exe, it works fine:

C:\>var.exe


C:\>set something=x

C:\>var.exe
x

But if you run the same application from another console (or without one), the variable won't be set.

It seems you can't set environment variable globally using the set command. But you can do it from .Net application using Environment.SetEnvironmentVariable() (by specifying the target parameter) or manually from Windows in the “Environment variables” window.

Upvotes: 2

Wojteq
Wojteq

Reputation: 1183

set command sets the variable only in console scope. Instead of doing this you can set this variable in system properties.

Upvotes: 1

Related Questions