Search code examples
c#environment-variablesconsole-applicationsystem.reflection

print all System.Environment information using System.Reflection


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?


Solution

  • 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"];