• 0

Free C# System Information Class Library


Question

I wrote this class so anyone looking to get information about their system would'nt have to go searching thru the MSDN Documentation.

The compiled dll is attached, just reference the dll and use the class. The class iis sealed, so all the properties are static.

Source is also available. I'm still adding more properties and methods to class.

Anyone who wants to extend the class's functionality, feel free to do so. :D

<pre>

/* Description: System Information Class File

 * Author: Pierre Walker

 * Email: [email]pierrejw@optonline.net[/email]

 * 

 * All Properties are Read Only

 * 

 * Class Properties:	UpTime - returns the time since the computer was last rebooted

 *      CpuSpeed - returns the speed of the CPU

 *      CpuId - returns CPU Identification

 *      OsVersion - returns the current os

 *      ComputerName - returns the NetBios name of the computer

 *      UserName - returns the name of the user logged on

 *      TotalPhysicalMemory - returns the total amount of RAM

 *      FreePhysicalMemory - returns the total amount of free RAM

 *      TotalPageFile - returns total amount of page file 

 *     	 FreePageFile - returns total amount of free page file 

 *      TotalVirtualMemory - returns total amount of virtual memory

 *      FreeVirtualMemory - returns total amount of free virtual memory

 *      MemoryLoad - returns the percent of memory being used

 * 

 * Usage: Class should be compiled as a class library (dll)

 *    Add a the dll as a reference to the project

 * 

 *    Use SysInfo to get properties (considering that the using DevGroupInc.SystemInfo 

 *        namespace is included or use the full qualified name (DevGroupInv.SystemInfo.SysInfo

 * 

 *    Class is a sealed and is made up of static properties

 */ 



using System;

using Microsoft.Win32;

using System.Runtime.InteropServices;

using System.Management;

using System.Windows.Forms;



namespace DevGroupInc

{

	namespace SystemInfo

	{

  sealed public class SysInfo

  {

 	 [StructLayout(LayoutKind.Sequential)] 

 	 private struct MEMORYSTATUS

 	 {

    public int  dwLength;

    public int  dwMemoryLoad;

    public int  dwTotalPhys;

    public int  dwAvailPhys;

    public int  dwTotalPageFile;

    public int  dwAvailPageFile;

    public int  dwTotalVirtual;

    public int  dwAvailVirtual;

 	 }

 	 

 	 [DllImport("kernel32.dll")]

 	 private  static extern void  GlobalMemoryStatus(ref  MEMORYSTATUS lpBuffer);



 	 public SysInfo()

 	 {

 	 

 	 }

 	 

 	 public static string UpTime

 	 {

    get

    {

   	 string uptime;



   	 TimeSpan ts = TimeSpan.FromMilliseconds(Environment.TickCount);



   	 int mins = ts.Minutes;

   	 int hours = ts.Hours;

   	 int days = ts.Days;

   	 int secs = ts.Seconds;

 	 

   	 uptime = ts.Days + " days, " + ts.Hours + " hrs, " + ts.Minutes + " mins";

 	 

   	 return uptime;

    }

 	 }



 	 public static string CpuSpeed

 	 {

    get

    {

   	 string cpuSpd;



   	 RegistryKey oRegKey = Registry.LocalMachine;

   	 oRegKey = oRegKey.OpenSubKey(@"hardwaredescriptionsystemcentralprocessor");



   	 Object cpuSpeed = oRegKey.GetValue("~mhz");

 	 

   	 cpuSpd = cpuSpeed.ToString() + " Mhz";



   	 return cpuSpd;

    }

 	 }



 	 public static string CpuId

 	 {

    get

    {

   	 string proc;

 	 

   	 RegistryKey oRegKey = Registry.LocalMachine;

   	 oRegKey = oRegKey.OpenSubKey(@"hardwaredescriptionsystemcentralprocessor");



   	 Object processor = oRegKey.GetValue("identifier");  



   	 proc = processor.ToString();



   	 return proc;

    }

 	 }



 	 public static string OsVersion

 	 {

    get

    {

   	 string platformId = Environment.OSVersion.Platform.ToString();

   	 int majorVer = Environment.OSVersion.Version.Major;

   	 int minorVer = Environment.OSVersion.Version.Minor;



   	 string osVersion = "";

 	 

   	 switch(platformId)

   	 {

      case "Win32Windows":

      {

     	 if(majorVer >= 4 && minorVer == 0)

     	 { 	 

        osVersion = "Windows 95";

        break;

     	 }

     	 if(majorVer >= 4 && (minorVer > 0 && minorVer < 90))

     	 {

        osVersion = "Windows 98";

        break;

     	 }

     	 if(majorVer >= 4 && (minorVer > 0 && minorVer >= 90))

     	 {

        osVersion = "Windows Me";

        break;

     	 }

     	 break;

      }

      case "Win32NT" :

      {

     	 if(majorVer <= 4 && minorVer == 0)

     	 {

        osVersion = "Windows NT";

        break;

     	 }

     	 if(majorVer == 5 && minorVer == 0)

     	 {

        osVersion = "Windows 2000";

        break;

     	 }

     	 if(majorVer == 5 && minorVer > 0)

     	 {

        osVersion = "Windows XP";

        break;

     	 }

     	 break;

      }

   	 }

   	 return osVersion;

    }

 	 }



 	 public static string ComputerName

 	 {

    get

    {

   	 return Environment.MachineName;

    }

 	 }



 	 public static string UserName

 	 {

    get

    {

   	 return Environment.UserName;

    }

 	 }



 	 public static string TotalPhysicalMemory

 	 {

    get

    {

   	 int totalPhysicalMem;



   	 MEMORYSTATUS lpMemStat = new MEMORYSTATUS();

   	 GlobalMemoryStatus(ref lpMemStat);



   	 totalPhysicalMem = lpMemStat.dwTotalPhys / 1024;



   	 totalPhysicalMem = (totalPhysicalMem / 1024) + 1;



   	 return totalPhysicalMem .ToString("0.00") + " MB";

    }

 	 }



 	 public static string FreePhysicalMemory

 	 {

    get

    {

   	 float availableMem;



   	 MEMORYSTATUS lpMemStat = new MEMORYSTATUS();

   	 GlobalMemoryStatus(ref lpMemStat);



   	 availableMem = lpMemStat.dwAvailPhys / 1024;



   	 availableMem = (availableMem / 1024);

 	 

   	 return availableMem.ToString("0.00") + " MB";

    }

 	 }



 	 public static string TotalPageFile

 	 {

    get

    {

   	 float totalPageFile = 0;

   	 try

   	 {

      MEMORYSTATUS lpMemStat = new MEMORYSTATUS();

      GlobalMemoryStatus(ref lpMemStat);



      totalPageFile = lpMemStat.dwTotalPageFile / 1024;



      totalPageFile = totalPageFile / 1024;

   	 }

   	 catch(Exception e)

   	 {

      MessageBox.Show(e.Message);

   	 }



   	 return totalPageFile.ToString("0.00") + " MB";

    }

 	 }



 	 public static string FreePageFile

 	 {

    get

    {

   	 float availablePageFile = 0;

   	 try

   	 {

      MEMORYSTATUS lpMemStat = new MEMORYSTATUS();

      GlobalMemoryStatus(ref lpMemStat);



      availablePageFile = lpMemStat.dwAvailPageFile  / 1024;



      availablePageFile = availablePageFile / 1024;

   	 }

   	 catch(Exception e)

   	 {

      MessageBox.Show(e.Message);

   	 }



   	 return availablePageFile.ToString("0.00") + " MB";

    }

 	 }



 	 public static string TotalVirtualMemory

 	 {

    get

    {

   	 object Mem = 0;

   	 UInt64 totalVirtualMem = 0;



   	 ManagementObjectSearcher mQueryOS = new ManagementObjectSearcher("Select totalVirtualMemorySize From Win32_OperatingSystem");



   	 ManagementObjectCollection mCollectionOS = mQueryOS.Get();



   	 foreach(ManagementObject mo in mCollectionOS)

   	 {



      Mem = mo["totalVirtualMemorySize"];

   	 }



   	 Mem = Convert.ToUInt64(Mem)/ 1024;

   	 totalVirtualMem = Convert.ToUInt64(Mem);



   	 return totalVirtualMem.ToString("0.00") + " MB";

    }

 	 }



 	 public static string FreeVirtualMemory

 	 {

    get

    {

   	 object Mem = 0;

   	 UInt64 availableVirtualMem = 0;



   	 ManagementObjectSearcher mQueryOS = new ManagementObjectSearcher("Select FreeVirtualMemory From Win32_OperatingSystem");



   	 ManagementObjectCollection mCollectionOS = mQueryOS.Get();



   	 foreach(ManagementObject mo in mCollectionOS)

   	 {



      Mem = mo["FreeVirtualMemory"];

   	 }



   	 Mem = Convert.ToUInt64(Mem)/ 1024;

   	 availableVirtualMem = Convert.ToUInt64(Mem);



   	 return availableVirtualMem.ToString("0.00") + " MB";

    }

 	 }



 	 public static string MemoryLoad

 	 {

    get

    {

   	 float memoryLoad = 0;

   	 try

   	 {

      MEMORYSTATUS lpMemStat = new MEMORYSTATUS();

      GlobalMemoryStatus(ref lpMemStat);



      memoryLoad = lpMemStat.dwMemoryLoad;

   	 }

   	 catch(Exception e)

   	 {

      MessageBox.Show(e.Message);

   	 }

   	 return memoryLoad.ToString() + " %";

    }

 	 }

  }

	}

}

</pre>

12 answers to this question

Recommended Posts

  • 0

First of all, this is a great class. Thanks!

Some suggestions:

1. The class would be of more use if every property weren't a string:

UpTime should be a TimeSpan.

CpuSpeed should be a int (with a MHz unit)

The memory properties (TotalPhysicalMemory, FreePhysicalMemory, TotalPageFile, FreePageFile, TotalVirtualMemory) should be ints (with a MB unit).

MemoryLoad should be a double (0-1).

Note that a lot of the time these properties will be used as strings, but it makes a lot of tasks easier if you use their actual types (converting units, say).

2. You may know this, but instead of:

namespace DevGroupInc {

namespace SystemInfo {

you can use namespace DevGroupInc.SystemInfo {

  • 0

I have the most comprehensive system information utility for .NET, it's called WMI Explorer, but it's not open source and I can't give you any more details about it because it's part of my company's next product. anyway, WMI is the ultimate technology for system information, so keep that in mind. Try not to use Windows APIs.

Try not to use the registry for CPU information because that information doesn't exist on Win9x. I am experienced in this sort of thing, I've been working on a benchmarking program for 3 years now. A new release of it is coming soon, called Dacris Benchmarks .NET, it will be built entirely with .NET and C#, no Win32 APIs. Find more information at

http://www.dacris.com/en/bmarknet.htm

To become a beta tester, send an email request to contact@dacris.com .

P.S. I can give you more advice and if you run into trouble, you can always email me at dacrisxp@hotmail.com . I used to want to make a class library exactly like this (for system information) but I gave up because I found WMI.

I just had to open my big mouth didn't I?

Fools speak because they have to say something. Wisemen speak because they have something to say.

  • 0

The reason why I used string for all the properties was because all the values will be calculated and formated inside the class.

Also cpu speed is aquired from the registry as a string and I did'nt want to convert to an int.

About the namespace issue, I will be adding more class to the namespace devgroupinc and also SystemInfo.

  • 0

dacris2000 now that you told me about the cpu registry not being available in win9x, I will have to rewrite this using WMI.

BTW the reason I did'nt use WMI, was because I wrote the class before I found out about WMI.

To use, compile as a dll then add a reference to the project you want to use the dll in.

  • 0
  Quote
The reason why I used string for all the properties was because all the values will be calculated and formated inside the class.
But what about users of your class? What if someone wants to write a program that sounds an alert when memory levels get too low? Will they have to parse a string to do it (not easy, especially considering you might change the units later on)?
  Quote
Also cpu speed is aquired from the registry as a string and I did'nt want to convert to an int.
Why not?
  Quote
About the namespace issue, I will be adding more class to the namespace devgroupinc and also SystemInfo.
If you are going to be adding more namespaces to the same file, then fair enough.

On a different note, this might be a good class to include in the Genghis project (http://www.genghisgroup.com).

  • 0
  Quote
Looks excellent! Can't wait to get home and give it a try. Maybe i'll port it over to VB.NET too.

/me slaps The_Unknown!!!!!!!!!!!!!!!

You didn't get the concept of .NET yet, huh?

You don't need to port anything. Compile it into a DLL or add the .CS to your VB.NET project (if you dont want a DLL), and basta. Language independence.

  • 0
  Quote
Originally posted by devgrp

Anyone who wants to extend the class's functionality, feel free to do so. :D

Okay! :D

I made some changes to this. Most importantly, detecting the CPU information without the aid of the registry.

Secondly, I made changes to the OS detection to get service pack numbers, differentiate between different versions of 95 and 98 (95B, 98SE, etc) and check for XP Home or Pro. Beware, however, this may not work as intended. I don't have XP Home, and I have no way to test.

My modifications, as well as a precompiled assembly is at:

http://members.shaw.ca/danny-smurf/sysinfo/sysinfo.zip

Use of the CPUInfo class absolutely requires the included stdiag.dll file.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • How long until the Windows 11 hating bots come and say that Windows 11 committed atrocities against their family and their dog?
    • If you never consented to cookies (required), and you're blocking ad domains and you never see ads on Neowin (despite whitelisting) wouldn't you agree the adblock popup notice is doing what it is supposed to do?
    • It's just exactly what it looks like. The facial polygon count is low and rigid, and the background appears heavily blurred. You can clearly see jagged edges around the car window as it exits the tunnel. The character animations feel like they're from 2010. The Xbox Series S has performance similar to an RX 570 or GTX 1660, which is disappointing and contributes to holding back game development. The Switch GPU is roughly on par with a 2050 or 3050, which is even worse. Developers have often discussed the challenges and limitations of developing for outdated/low end hardware, noting how it can hold back progress. This isn't a new issue. I am sure we will hear about the "Challenges" they had to overcome here and sacrifices that had to be made for compatibility with the outdated/low end hardware.
    • Whether on Edge or Firefox, I don't get this prompt to accept cookies, because they're already whitelisted by the Cookie AutoDelete extension, both on Edge and Firefox. And, as I've already said, using Edge (yesterday), Neowin isn't whitelisted (I never use Edge), and I didn't get that pop-up. And this morning, after checking, still no pop-up, and everything is activated, the proof (screenshot), I have no ads on the right side. I must add that I block the following trackers Doubleclick, and Scorecardresearch, as well as a few other undesirable ones, via the Host file. Back to square one! After all, it's a simple click to continue, so I'll do with it. 🤷🏽‍♂️ Thank You
    • F'ing FIIINALLY! Much easier to ask people what their hardware is 🙏 and could also very well just say DDR4 or DDR5 without asking to bloat it too much. I'm also curious to see what it will say in More device info...
  • Recent Achievements

    • Week One Done
      jbatch earned a badge
      Week One Done
    • First Post
      Yianis earned a badge
      First Post
    • Rookie
      GTRoberts went up a rank
      Rookie
    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      400
    2. 2
      +FloatingFatMan
      178
    3. 3
      snowy owl
      171
    4. 4
      ATLien_0
      169
    5. 5
      Xenon
      134
  • Tell a friend

    Love Neowin? Tell a friend!