Results 1 to 4 of 4

Thread: FYI:: Custom System Information Class

Threaded View

  1. #1

    Thread Starter
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256

    FYI:: Custom System Information Class

    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.

    Code:
    /* Description: System Information Class File
     * Author: Pierre Walker
     * Email: [email protected]
     * 
     * 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(@"hardware\description\system\centralprocessor\0");
    
    					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(@"hardware\description\system\centralprocessor\0");
    
    					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() + " %";
    				}
    			}
    		}
    	}
    }
    The Attached file is not a text file, you have to rename it to SysInfo.dll. I had to do it that way to upload it.
    Attached Files Attached Files
    Dont gain the world and lose your soul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width