C# SystemInfo Klasse

01/18/2012 15:31 boxxiebabee#1
Moin,

hier ist ne kleine Klasse von mir um ein paar Informationen vom System zu bekommen.

Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Windows.Forms;
using Microsoft.Win32;

namespace Sors
{
    internal class SystemInfo
    {
        #region User

        //Returns the user name
        public string UserName()
        {
            return Environment.UserName;
        }

        //Returns the user domain name
        public string UserDomain()
        {
            return Environment.UserDomainName;
        }

        #endregion

        #region Operating System

        //Returns the operating system name
        public string OsName()
        {
            var name =
                (from x in
                     new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>()
                 select x.GetPropertyValue("Caption")).First();
            return name != null ? name.ToString() : "Unknown";
        }

        //Returns the operating system version
        public string OsVersion()
        {
            return Environment.OSVersion.ToString();
        }

        //Returns the operating system uptime
        public int OsTime()
        {
            return (Environment.TickCount/60);
        }

        #endregion

        #region System

        //Returns the system directory
        public string SystemDirectory()
        {
            return Environment.SystemDirectory;
        }

        //Returns the available ram in megabyte
        public int AvailableRam()
        {
            var pc = new PerformanceCounter("Memory", "Available Bytes");
            return Convert.ToInt32(Math.Round(pc.NextValue()/1048576));
        }

        //Returns all running processes
        public List<string> RunningProcesses()
        {
            Process[] processlist = Process.GetProcesses();
            return processlist.Select(theprocess => theprocess.ProcessName).ToList();
        }

        #endregion

        #region Machine

        //Returns the machine name
        public string MachineName()
        {
            return Environment.MachineName;
        }

        #endregion

        #region Hardware

        //Returns the graphic card name
        public string GraphicCardName()
        {
            var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DisplayConfiguration");
            return (from ManagementObject mo in searcher.Get()
                    from property in
                        mo.Properties.Cast<PropertyData>().Where(property => property.Name == "Description")
                    select property.Value.ToString()).FirstOrDefault();
        }

        //Returns the processor name
        public string CpuName()
        {
            RegistryKey processorName =
                Registry.LocalMachine.OpenSubKey(@"Hardware\Description\System\CentralProcessor\0",
                                                 RegistryKeyPermissionCheck.ReadSubTree);
            return processorName != null
                       ? (processorName.GetValue("ProcessorNameString") != null
                              ? processorName.GetValue("ProcessorNameString").ToString()
                              : null)
                       : null;
        }

        //Returns the prozessor frequency in megacycles
        public int CpuFrequency()
        {
            RegistryKey cpuFrequency =
                Registry.LocalMachine.OpenSubKey(@"Hardware\Description\System\CentralProcessor\0",
                                                 RegistryKeyPermissionCheck.ReadSubTree);
            return cpuFrequency != null
                       ? (cpuFrequency.GetValue("~MHz") != null ? (int) cpuFrequency.GetValue("~MHz") : 0)
                       : 0;
        }

        //Returns the total physical ram amount in megabyte
        public int TotalRam()
        {
            var mc = new ManagementClass("Win32_ComputerSystem");
            ManagementObjectCollection moc = mc.GetInstances();
            return (from ManagementObject item in moc
                    select
                        Convert.ToInt32(
                            Math.Round(Convert.ToDouble(item.Properties["TotalPhysicalMemory"].Value)/1048576, 2))).
                FirstOrDefault();
        }

        //Returns the sound card name
        public string SoundCardName()
        {
            var searcher = new ManagementObjectSearcher("Select ProductName from Win32_SoundDevice");
            return (from ManagementObject mo in searcher.Get()
                    from property in
                        mo.Properties.Cast<PropertyData>().Where(property => property.Name == "ProductName")
                    select property.Value.ToString()).FirstOrDefault();
        }

        #endregion

        #region Resolution

        //Returns the screen resolution height
        public int ScreenResolutionHeight()
        {
            return Screen.PrimaryScreen.Bounds.Height;
        }

        //Returns the screen resolution width
        public int ScreenResolutuinWidth()
        {
            return Screen.PrimaryScreen.Bounds.Width;
        }

        #endregion
    }
}