Deleted
Deleted

Reputation: 4227

How to read a user environment variable in C#?

How can I read a user specific environment variable? I know how to get a system wide one, like

Environment.GetEnvironmentVariable("SOMETHING");

Thanks in advance!

Upvotes: 26

Views: 27425

Answers (4)

bin4rym4ge
bin4rym4ge

Reputation: 21

var UserName = Environment.GetEnvironmentVariable("username");

Upvotes: 1

Jaroslav Jandek
Jaroslav Jandek

Reputation: 9563

Use the other overload of the Environment.GetEnvironmentVariable Method that lets you specify the EnvironmentVariableTarget.

Environment.GetEnvironmentVariable(variable, target);

target can be:
EnvironmentVariableTarget.Process,
EnvironmentVariableTarget.User,
EnvironmentVariableTarget.Machine.

Upvotes: 42

Hans Olsson
Hans Olsson

Reputation: 55049

It's the same method, just set the second parameter to be User as:

System.Environment.GetEnvironmentVariable("varName", EnvironmentVariableTarget.User);

Upvotes: 7

Giorgi
Giorgi

Reputation: 30883

Use second overload of GetEnvironmentVariable which let's you specify EnvironmentVariableTarget.

Upvotes: 0

Related Questions