获取Windows机器上的pc(系统)信息 – C#脚本
有没有办法通过使用c#脚本获取以下信息。
PC名称服务标签CPU类型c:驱动器,已安装的RAM,操作系统名称,操作系统产品密钥,Office版本和Office产品密钥的CPU速度大小。
谢谢。
WMI正是您所需要的。
https://www.codeproject.com/KB/cs/EverythingInWmi02.aspx
让我添加第3部分的链接,它通过WMI专注于硬件
https://www.codeproject.com/KB/cs/EverythingInWmi03.aspx
MSDN也是WMI范围的一个很好的资源……
https://msdn.microsoft.com/en-us/library/aa394554(v=vs.85).aspx
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("nnDisplaying Processor Name...."); RegistryKey processor_name = Registry.LocalMachine.OpenSubKey(@"HardwareDescriptionSystemCentralProcessor ", 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. } } } } }
有一个名为MissingLinq.Linq2Management的nuget包,它将WMI的所有内容都包装成一个很好的强类型对象。 看起来很不错。
https://missinglinq.codeplex.com/
为此,在您的参考中添加Microsoft.VB。 要添加此右键单击解决方案探索的Refrence文件夹,然后单击添加参考,然后单击.NET并单击Microsoft.visualBasic,然后单击确定。 执行此操作后,这样:
using Microsoft.VisualBasic.Devices; public void getSystemDetails() { UserName.Text = Environment.UserName; // User name of PC LabelOS.Text = getOSInfo(); // OS version of pc MachineTxt.Text = Environment.MachineName;// Machine name string OStype = ""; if (Environment.Is64BitOperatingSystem) { OStype = "64-Bit, "; } else { OStype = "32-Bit, "; } OStype += Environment.ProcessorCount.ToString() + " Processor"; label8.Text = OStype; // Processor type ulong toalRam = cinfo.TotalPhysicalMemory; double toal = Convert.ToDouble(toalRam / (1024 * 1024)); int t = Convert.ToInt32(Math.Ceiling(toal / 1024).ToString()); label6.Text = t.ToString() + " GB";// ram detail } public string getOSInfo() { //Get Operating system information. OperatingSystem os = Environment.OSVersion; //Get version information about the os. Version vs = os.Version; //Variable to hold our return value string operatingSystem = ""; if (os.Platform == PlatformID.Win32Windows) { //This is a pre-NT version of Windows switch (vs.Minor) { case 0: operatingSystem = "95"; break; case 10: if (vs.Revision.ToString() == "2222A") operatingSystem = "98SE"; else operatingSystem = "98"; break; case 90: operatingSystem = "Me"; break; default: break; } } else if (os.Platform == PlatformID.Win32NT) { switch (vs.Major) { case 3: operatingSystem = "NT 3.51"; break; case 4: operatingSystem = "NT 4.0"; break; case 5: if (vs.Minor == 0) operatingSystem = "Windows 2000"; else operatingSystem = "Windows XP"; break; case 6: if (vs.Minor == 0) operatingSystem = "Windows Vista"; else operatingSystem = "Windows 7 or Above"; break; default: break; }
你有PC名称作为你想要的值,所以你可以从Environment.MachineName
得到这个,如果你想要本地计算机,或者你可以做IPHostEntry hostEntry = Dns.GetHostEntry(ip);
然后string host = hostEntry.HostName;
如果您只拥有其IP,则使用DNS解析远程计算机的名称。
假设您需要远程计算机,在检查远程注册表正在运行后,您可以从注册表中获取某些信息:
ServiceController sc = new ServiceController("RemoteRegistry", computer); if (sc.Status.Equals(ServiceControllerStatus.Running)) { // do your stuff }
如果发现停止,你可以启动它:
if (sc.Status.Equals(ServiceControllerStatus.Stopped) || sc.Status.Equals(ServiceControllerStatus.StopPending)) { sc.Start(); }
将此using
语句添加到页面顶部:
using Microsoft.Win32;
对于计算机名称,您可以转到HKEY_LOCAL_MACHINE SYSTEM CurrentControlSet Control ComputerName ActiveComputerName:
string path = @"HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlComputerNameActiveComputerName"; RegistryKey rk = Registry.OpenRemoteBaseKey(RegistryHive.LocalMachine, computer).OpenSubKey(path); string pcName = rk.GetValue("computerName").ToString();
对于任何本地注册表命令,只需删除Registry.OpenRemoteBaseKey(
和, computer)
。
对于CPU名称:
string path = @"HKEY_LOCAL_MACHINEHARDWAREDESCRIPTIONSystemCentralProcessor "; RegistryKey rk = Registry.OpenRemoteBaseKey(RegistryHive.LocalMachine, computer).OpenSubKey(path); string cpuName = rk.GetValue("processorNameString").ToString();
对于操作系统名称和密钥:
string path = @"SOFTWAREMicrosoftWindows NTCurrentVersion"; RegistryKey rk = Registry.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);
从Geeks With Blogs获取产品密钥:
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); }
这让那些艰难的事情发生了变化。 重点是,注册表是你的朋友。
上述就是C#学习教程:获取Windows机器上的pc(系统)信息 – C#脚本分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注---计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1009657.html