Get pc (system) information on windows machine
WMI is what you're looking for.
http://www.codeproject.com/KB/cs/EverythingInWmi02.aspx
Let me add the link to Part 3 too, which concentrates on hardware via WMI
http://www.codeproject.com/KB/cs/EverythingInWmi03.aspx
MSDN is also a great resource for WMI scopes...
http://msdn.microsoft.com/en-us/library/aa394554(v=vs.85).aspx
There is a nuget package called MissingLinq.Linq2Management
that has wrapped pretty much everything about WMI into a nice strongly typed object. Seems pretty nice.
https://missinglinq.codeplex.com/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management; //This namespace is used to work with WMI classes. For using this namespace add reference of System.Management.dll .
using Microsoft.Win32; //This namespace is used to work with Registry editor.
namespace OperatingSystemInfo1
{
class TestProgram
{
static void Main(string[] args)
{
SystemInfo si = new SystemInfo(); //Create an object of SystemInfo class.
si.getOperatingSystemInfo(); //Call get operating system info method which will display operating system information.
si.getProcessorInfo(); //Call get processor info method which will display processor info.
Console.ReadLine(); //Wait for user to accept input key.
}
}
public class SystemInfo
{
public void getOperatingSystemInfo()
{
Console.WriteLine("Displaying operating system info....\n");
//Create an object of ManagementObjectSearcher class and pass query as parameter.
ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_OperatingSystem");
foreach (ManagementObject managementObject in mos.Get())
{
if (managementObject["Caption"] != null)
{
Console.WriteLine("Operating System Name : " + managementObject["Caption"].ToString()); //Display operating system caption
}
if (managementObject["OSArchitecture"] != null)
{
Console.WriteLine("Operating System Architecture : " + managementObject["OSArchitecture"].ToString()); //Display operating system architecture.
}
if (managementObject["CSDVersion"] != null)
{
Console.WriteLine("Operating System Service Pack : " + managementObject["CSDVersion"].ToString()); //Display operating system version.
}
}
}
public void getProcessorInfo()
{
Console.WriteLine("\n\nDisplaying Processor Name....");
RegistryKey processor_name = Registry.LocalMachine.OpenSubKey(@"Hardware\Description\System\CentralProcessor\0", RegistryKeyPermissionCheck.ReadSubTree); //This registry entry contains entry for processor info.
if (processor_name != null)
{
if (processor_name.GetValue("ProcessorNameString") != null)
{
Console.WriteLine(processor_name.GetValue("ProcessorNameString")); //Display processor ingo.
}
}
}
}
}
You have PC Name as a value you want, so you can get this from Environment.MachineName
if you want the local computer, or you can do IPHostEntry hostEntry = Dns.GetHostEntry(ip);
then string host = hostEntry.HostName;
to use DNS to resolve a remote computer's name if you only have its IP.
You can get certain information from the registry, after you check that Remote Registry is running, assuming you want a remote computer:
ServiceController sc = new ServiceController("RemoteRegistry", computer);
if (sc.Status.Equals(ServiceControllerStatus.Running))
{
// do your stuff
}
And you can start it if found stopped:
if (sc.Status.Equals(ServiceControllerStatus.Stopped) ||
sc.Status.Equals(ServiceControllerStatus.StopPending))
{
sc.Start();
}
Add this using
statement to the top of your page:
using Microsoft.Win32;
For the Computer name, you can go to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName:
string path = @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName";
RegistryKey rk = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, computer).OpenSubKey(path);
string pcName = rk.GetValue("computerName").ToString();
For any local registry commands, just remove RegistryKey.OpenRemoteBaseKey(
and , computer)
- it becomes:
RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(path);
The RegistryView.Registry64
is usually not required (you can use RegistryView.Default
instead), but can be necessary if building a 32-bit app that needs to reach into a registry on a 64-bit OS. Instead of all in one line, you could also do something like this, for example:
using (var root = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
{
using (var key = root.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion", false))
{
var registeredOwner = key.GetValue("RegisteredOwner");
}
}
Credit: https://social.msdn.microsoft.com/Forums/en-US/ea997421-4d55-49db-97ad-cf629c65577b/registrylocalmachineopensubkey-does-not-return-all-values?forum=csharpgeneral
For the CPU name:
string path = @"HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0";
RegistryKey rk = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, computer).OpenSubKey(path);
string cpuName = rk.GetValue("processorNameString").ToString();
For the OS Name and key:
string path = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion";
RegistryKey rk = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, computer).OpenSubKey(path);
string osName = rk.GetValue("productName").ToString();
string servicePack = rk.GetValue("CSDVersion").ToString();
byte[] digitalProductId = registry.GetValue("DigitalProductId") as byte[];
string osProductKey = DecodeProductKey(digitalProductId);
From Geeks With Blogs for getting product keys:
public static string DecodeProductKey(byte[] digitalProductId)
{
// Offset of first byte of encoded product key in
// 'DigitalProductIdxxx" REG_BINARY value. Offset = 34H.
const int keyStartIndex = 52;
// Offset of last byte of encoded product key in
// 'DigitalProductIdxxx" REG_BINARY value. Offset = 43H.
const int keyEndIndex = keyStartIndex + 15;
// Possible alpha-numeric characters in product key.
char[] digits = new char[]
{
'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'M', 'P', 'Q', 'R',
'T', 'V', 'W', 'X', 'Y', '2', '3', '4', '6', '7', '8', '9',
};
// Length of decoded product key
const int decodeLength = 29;
// Length of decoded product key in byte-form.
// Each byte represents 2 chars.
const int decodeStringLength = 15;
// Array of containing the decoded product key.
char[] decodedChars = new char[decodeLength];
// Extract byte 52 to 67 inclusive.
ArrayList hexPid = new ArrayList();
for (int i = keyStartIndex; i <= keyEndIndex; i++)
{
hexPid.Add(digitalProductId[i]);
}
for (int i = decodeLength - 1; i >= 0; i--)
{
// Every sixth char is a separator.
if ((i + 1) % 6 == 0)
{
decodedChars[i] = '-';
}
else
{
// Do the actual decoding.
int digitMapIndex = 0;
for (int j = decodeStringLength - 1; j >= 0; j--)
{
int byteValue = (digitMapIndex << 8) | (byte)hexPid[j];
hexPid[j] = (byte)(byteValue / 24);
digitMapIndex = byteValue % 24;
decodedChars[i] = digits[digitMapIndex];
}
}
}
return new string(decodedChars);
}
That gets the hard ones out of the way. Point is, the registry is your friend.