Environment Values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • boenz666
    New Member
    • Dec 2011
    • 1

    Environment Values

    My task is to read the environment variables and their values and show them in a listBox. I don't have any problems with getting the variables, but I'm not able to get their values...

    Code:
    switch (comboBoxRegion.Text)
    {
        case "Global":
            aktenv = new GlobalEnv();
            aktGlobal = true;
            break;
        case "User":
            aktenv = new UserEnv();
            aktGlobal = false;
            break;
        FillComboBox();
    }
    
    private void FillComboBox()
    {
        if (aktGlobal)
        {
            foreach (string var in aktenv.EnumGlobalVars())
            {
                listBox1.Items.Add(var);
            }
        }
    
        else
        {
            foreach (string var in aktenv.EnumUserVars())
            {
                listBox1.Items.Add(var);
            }
        }
    }
  • Fr33dan
    New Member
    • Oct 2008
    • 57

    #2
    I cannot find any reference to the GlobalEnv or UserEnv objects you construct in your switch online and cannot compile your code.

    You should be able to use System.Environm ent.GetEnvironm entVariables() to get a dictionary of environment variables, then just iterate through the the values collection instead (Also it's bad code to have the loop twice so I reworked it to only have one loop with different parameters if depending on what's needed):

    Code:
    System.Collections.ICollection vars;
    if (aktGlobal)
    {
        vars = System.Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine).Values;
        
    }
    else
    {
        vars = System.Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User).Values;
    }   
    foreach (string var in vars)
    {
        listBox1.Items.Add(var);
    }

    Comment

    Working...