Friday, April 12, 2013

How To- Get System Information using C#.

Here, I am going to explain you how to get the system information like OS information, Processor information etc using C#.

In my previous posts, I explained Hard drive information using C#, Create Directory/Folder using C#, Check Internet Connection using C#, SQL Server Database BackUp using C#, Partial Methods, Contextual Keyword, C# Static Methods and some other articles related to C#, ASP.Net and SQL Server .

For retrieving system information, we will use System.Management namespace.Using this namespace we can get lot of information about the system. For using this namespace follow the steps given below.

  • Add the reference of System.Management in your application. Go to Add Reference->.Net Tab

    Add Reference
  • Now add System.Management  namespace in your code.

    using System.Management;
  • After adding the namespace, write the following method to get the OS Information of your system
    public static void GetOSInformation()
    {
                try
                {
                    ManagementObjectSearcher search = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
                    Console.WriteLine("OS Version : {0}", Environment.Version);
                    foreach (ManagementObject obj in search.Get())
                    {
                        PropertyDataCollection searcherProperties = obj.Properties;
                        foreach (PropertyData sp in searcherProperties)
                        {
                            if (obj[sp.Name] != null)
                            {
                                Console.WriteLine("{0} : {1}", sp.Name, obj[sp.Name].ToString());
                            }
                        }                   
                    }
                    Console.ReadKey();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error:{0}", ex.Message);
                    Console.ReadKey();
                }
    }

    Here I am show you mthod for getting the OS information of system. There are some other things like processor, devices etc. You can also get information about these things. For more information please download the sample code. It is a window application in which you can get code for all the options whose information you want to get.

Output of Sample Code

System Information using C#
I hope this will be helpful for you. I would like to have any feedback from you. Your valuable feedback, question, or comments about this article are always welcome.

No comments:

Post a Comment

^ Scroll to Top