11

We have a little task to print in a console window all the variables of the Environment class using reflection, but how to do so I don't even have a clue. I am sorry if I've written anything wrong here, I'm new to C#.

Of course I could use this kind of code, but that is not what is required from me.

string machineName = System.Environment.MachineName;
Console.WriteLine(machineName);

I searched Google so much and this is what I found, but I don't think this is what I need. I don't even know what I need.

System.Reflection.Assembly info = typeof(System.Int32).Assembly;
System.Console.WriteLine(info);

Any suggestions, clues?

Arm91
  • 125
  • 1
  • 1
  • 8
  • Look at the members in `typeof(Environment)` – SLaks Apr 20 '14 at 20:36
  • 1
    Other than the [tag:environment-variables] tag, this question seems to be pretty clearly asking how to dynamically query all the _properties_ of the `Environment` class. So why are two answers and a comment — not to mention the author themselves in accepting [this answer](https://stackoverflow.com/a/23187385/150605) — mistaking this for a question about how to read environment variables? If it was it should have been closed as a duplicate of something like [this](https://stackoverflow.com/q/185208/150605). "I don't even know what I need." seems to sum it up, so I'm voting to close, anyways. – Lance U. Matthews Jun 21 '22 at 01:05

3 Answers3

33

You don't need reflection here

foreach(DictionaryEntry e in System.Environment.GetEnvironmentVariables())
{
    Console.WriteLine(e.Key  + ":" + e.Value);
}

var compName = System.Environment.GetEnvironmentVariables()["COMPUTERNAME"];
L.B
  • 114,136
  • 19
  • 178
  • 224
  • 1
    You should probably use `Environment.GetEnvironmentVariable("COMPUTERNAME")` rather than an indexer, otherwise this answer is fine. If you are going to access it with the indexer, at least cache the result of `System.Environment.GetEnvironmentVariables()` – ScottishTapWater Jun 24 '22 at 13:57
4

Get the all public and static properties of Environment using GetProperties method, then display the name and the value of each property:

var properties = typeof(Environment)
                .GetProperties(BindingFlags.Public | BindingFlags.Static);

foreach(var prop in properties)
   Console.WriteLine("{0} : {1}", prop.Name, prop.GetValue(null));
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
-1

Although it's an old question, there is a neater possible answer based on the accepted answer, using Linq:

IEnumerable<string> environmentVariables = Environment.GetEnvironmentVariables()
   .Cast<DictionaryEntry>()
   .Select(de => $"{de.Key}={de.Value}");

Console.WriteLine("Environment Variables: " + string.Join(Environment.NewLine, environmentVariables));

Or, a shorter version of the same code:

Console.WriteLine("Environment Variables: " + string.Join(Environment.NewLine, 
    Environment.GetEnvironmentVariables()
       .Cast<DictionaryEntry>()
       .Select(de => $"{de.Key}={de.Value}")));
Aharon Ohayon
  • 1,171
  • 1
  • 17
  • 20
  • This had long been possible when [the accepted answer](https://stackoverflow.com/a/23187385/150605) was posted in 2014; LINQ was introduced with .NET 3.5 in 2007. The key point, as the other answer clearly demonstrates, is to call `GetEnvironmentVariables()`, from which one can enumerate `DictionaryEntry` values. I don't think this answers the question as-asked, anyways, which is about reading `Environment` _properties_, not environment _variables_. – Lance U. Matthews Jun 21 '22 at 01:12
  • I used the `GetEnvironmentVariables()` and iterated it using the `DictionaryEntry`, exactly like the accepted answer (which also didn't answer all the question). Regarding the Linq, that's a nice comment :) – Aharon Ohayon Jun 21 '22 at 01:20
  • "exactly like the accepted answer" — That's my point. Why are two answers demonstrating the same core concept needed? They're not. And how does the other answer not "answer all the question"? The only thing you've added here is `string.Join()`, which the other answer does fine without. You're not printing anything to the console, though, which, technically, _is_ part of the question. – Lance U. Matthews Jun 21 '22 at 01:30
  • The answer has its own benefit due to its implementation, especially using the cast. Regarding the console and the LINQ, I edited the answer to make it more accurate – Aharon Ohayon Jun 24 '22 at 12:19