Loop through IDictionary

Posted on 2009-05-07
Last Modified: 2012-05-06
I would like to list the content of the dictionary in the code in this post, but I can't seem to get it right. Could someone help me?
/// <summary>

        /// Get the machine level information.

        /// </summary>

        /// <returns></returns>

        public static IDictionary GetMachineInfo()


            // Get all the machine info.

            IDictionary machineInfo = new SortedDictionary<string, object>();

            machineInfo["Machine Name"] = Environment.MachineName;

            machineInfo["Domain"] = Environment.UserDomainName;

            machineInfo["User Name"] = Environment.UserName;

            machineInfo["CommandLine"] = Environment.CommandLine;

            machineInfo["ProcessorCount"] = Environment.ProcessorCount;

            machineInfo["OS Version Platform"] = Environment.OSVersion.Platform.ToString();

            machineInfo["OS Version ServicePack"] = Environment.OSVersion.ServicePack;

            machineInfo["OS Version Version"] = Environment.OSVersion.Version.ToString();

            machineInfo["OS Version VersionString"] = Environment.OSVersion.VersionString;

            machineInfo["System Directory"] = Environment.SystemDirectory;

            machineInfo["Memory"] = Environment.WorkingSet.ToString();

            machineInfo["Version"] = Environment.Version.ToString();

            machineInfo["Current Directory"] = Environment.CurrentDirectory;

            return machineInfo;


Open in new window

Question by:Ingve
LVL 96

Expert Comment

by:Bob Learned
ID: 24337277
Guess #1:

foreach (string key in machineInfo.Keys)
    string value = machineInfo[key];

Accepted Solution

drmason earned 500 total points
ID: 24340693
For a System.Collections.IDictionary:

foreach(DictionaryEntry de in machineInfo)
     DoSomething(de.Key, de.Value);

For a System.Collections.Generic.IDictionary<string,object> you would use:

foreach(KeyValuePair<string, object> kvp in machineInfo)
     DoSomething(kvp.Key, kvp.Value);

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Healthcare organizations in the United States must adhere to the guidance of both the HIPAA (Health Insurance Portability and Accountability Act) and HITECH (Health Information Technology for Economic and Clinical Health Act) for securing and protec…
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now