views:

36

answers:

1

App: C#, .NET4, Windows 7

When an app starts up, it gets a copy of the current environment variables and they stay constant for the life of that app. I want to know how to get the actual current environment variables, allowing me to react to changes.

Do I need to go to the registry? I've pondered spawning a cmd.exe and dumping the output of set, but I'd prefer something less hacky.

Thanks

+1  A: 

The following overload might give you what you want:

String Environment.GetEnvironmentVariable(String, EnvironmentVariableTarget)

Try passing EnvironmentVariableTarget.User or EnvironmentVariableTarget.Machine.

To get all of the variables, call

IDictionary Environment.GetEnvironmentVariables(EnvironmentVariableTarget)
Jeffrey L Whitledge
I've verified this does work for User variables, didn't test machine but from the docs I'd expect it would. Unfortunately, I also verified that the environment var I was looking for (CLIENTNAME) is only a Process variable. So much for an easy way to see who's remoted in to win7.
Tom
If the questions is, "How can find out who is remoted in to Windows 7?", then I would suggest looking into WMI http://msdn.microsoft.com/en-us/library/aa384642(v=VS.85).aspx.
Jeffrey L Whitledge