Let's know about ComputerInfo Class

Rajnilari2015
Posted by in C# category on for Beginner level | Points: 250 | Views : 861 red flag

ComputerInfo class provides properties for getting information about the computer's memory, loaded assemblies, name, and operating system. In this article we will look into the usage of the same.


 Download source code for Let's know about ComputerInfo Class

Recommendation
Read How to use Dynamic Data Masking of SQL Server 2016 before this article.

Introduction

ComputerInfo class provides properties for getting information about the computer's memory, loaded assemblies, name, and operating system. It is available under the Microsoft.VisualBasic assembly. In this article we will look into the usage of the same.

Straight to program

First of all we need to add a reference to Microsoft.VisualBasic.dll.

Find the Total Physical Memory of the computer

using Microsoft.VisualBasic.Devices;
using System;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            ComputerInfo CI = new ComputerInfo(); 

            //get total physical memory
            var totalPhysicalMemory = ((CI.TotalPhysicalMemory / (1024 * 1024)) * 0.001 + " GB").ToString();

            Console.WriteLine("The total physical memory is  {0}", totalPhysicalMemory);

            Console.ReadKey();

        }
    }
}

Output
---------
The total physical memory is  8.092 GB

The property TotalPhysicalMemory, gets the total amount of physical memory for the computer. It returns a ULong containing the number of bytes of physical memory for the computer.

Now we are first converting the value to Mega bytes(MB) and hence performing

CI.TotalPhysicalMemory / (1024 * 1024)

Once we obtain the result in Mega bytes(MB), we are converting the same to Giga bytes(GB).We know that

 1 MB = 0.001 GB

And hence, is the conversion.

Find the Total Virtual Memory of the computer

using Microsoft.VisualBasic.Devices;
using System;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            ComputerInfo CI = new ComputerInfo(); 

            //get total virtual memory
            var totalVirtualMemory = ((CI.TotalVirtualMemory / (1024 * 1024)) * 0.001 + " GB").ToString();

             Console.WriteLine("The total virtual memory is  {0}", totalVirtualMemory);

            Console.ReadKey();

        }
    }
}

Output
---------
The total virtual memory is  2.047 GB

The property TotalVirtualMemory, gets the total amount of physical memory for the computer. It returns a ULong containing the number of bytes of physical memory for the computer.

Find the Available Physical Memory of the computer

using Microsoft.VisualBasic.Devices;
using System;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            ComputerInfo CI = new ComputerInfo(); 

            //get total memory
            var availablePhysicalMemory = ((CI.AvailablePhysicalMemory / (1024 * 1024)) * 0.001 + " GB").ToString();

            Console.WriteLine("The available physical memory is  {0}", availablePhysicalMemory);

            Console.ReadKey();

        }
    }
}

Output
---------
The available physical memory is  3.692 GB

The property AvailablePhysicalMemory, gets the total amount of free physical memory for the computer. It returns a ULong containing the number of bytes of physical memory for the computer.

Find the Available Virtual Memory of the computer

using Microsoft.VisualBasic.Devices;
using System;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            ComputerInfo CI = new ComputerInfo(); 

            //get total memory
            var availableVirtualMemory = ((CI.AvailableVirtualMemory / (1024 * 1024)) * 0.001 + " GB").ToString();

            Console.WriteLine("The available virtual memory is  {0}", availableVirtualMemory);

            Console.ReadKey();

        }
    }
}

Output
---------
The available virtual memory is  1.864 GB

The property AvailableVirtualMemory, gets the total amount of computer's free virtual address space. It returns a ULong containing the number of bytes of the computer's free virtual address space.

Get Operating System Information(s)

using Microsoft.VisualBasic.Devices;
using System;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            ComputerInfo CI = new ComputerInfo(); 

            //get OS informations

            var osInformation = string.Format("OS FullName: {0}, OS Platform: {1}, OS Version: {2}", CI.OSFullName, CI.OSPlatform, CI.OSVersion);
           
            Console.WriteLine(osInformation);

            Console.ReadKey();

        }
    }
}
Output
---------
OS FullName: Microsoft Windows 8.1 Enterprise, OS Platform: Win32NT, OS Version: 6.2.9200.0

The property OSFullName, provides the full operating system name. It returns a string containing the operating-system name.

The property OSPlatform, provides the platform identifier for the operating system of the computer. It returns a string containing the platform identifier for the operating system of the computer, chosen from the member names of the System.PlatformID enumeration.

The property OSVersion, provides the version of the computer's operating system. It returns a string containing the current version number of the operating system.

Conclusion

Hope this will be helpful.Thanks for reading. Zipped file attached.

Recommendation
Read How to use STRING_SPLIT function of SQL Server 2016 after this article.
Page copy protected against web site content infringement by Copyscape

About the Author

Rajnilari2015
Full Name: Niladri Biswas (RNA Team)
Member Level: Platinum
Member Status: Member,Microsoft_MVP,MVP
Member Since: 3/17/2015 2:41:06 AM
Country: India
-- Thanks & Regards, RNA Team


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)