Created
August 31, 2024 03:57
-
-
Save QuiltMeow/48b23cd905ce366d7e17741f60d528d8 to your computer and use it in GitHub Desktop.
[WMI] Query Host And Monitor Serial ID
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Management; | |
using System.Text; | |
using static System.Management.ManagementObjectCollection; | |
namespace SerialQuery | |
{ | |
public class Program | |
{ | |
private const string OUTPUT_FILE = "SN.txt"; | |
public struct MonitorInformation | |
{ | |
public string connectHost; | |
public string makeModel; | |
public string serialNumber; | |
} | |
public static string convertWMIDataToString(object data) | |
{ | |
return Encoding.UTF8.GetString(((ushort[])data).Select(Convert.ToByte).ToArray()).TrimEnd('\0').TrimEnd(); | |
} | |
public static string queryWMIHost() | |
{ | |
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT SerialNumber FROM Win32_BIOS")) | |
using (ManagementObjectCollection collection = searcher.Get()) | |
using (ManagementObjectEnumerator enumerator = collection.GetEnumerator()) | |
{ | |
enumerator.MoveNext(); | |
ManagementBaseObject queryObject = enumerator.Current; | |
object serialNumber = queryObject["SerialNumber"]; | |
if (serialNumber == null) | |
{ | |
throw new KeyNotFoundException("找不到 SerialNumber 資料"); | |
} | |
return serialNumber.ToString(); | |
} | |
} | |
public static MonitorInformation queryWMIMonitor() | |
{ | |
MonitorInformation ret = new MonitorInformation(); | |
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"root\WMI", "SELECT __Server, UserFriendlyName, SerialNumberID FROM WmiMonitorID")) | |
using (ManagementObjectCollection collection = searcher.Get()) | |
using (ManagementObjectEnumerator enumerator = collection.GetEnumerator()) | |
{ | |
enumerator.MoveNext(); | |
ManagementBaseObject queryObject = enumerator.Current; | |
object connectHost = queryObject["__Server"]; | |
if (connectHost == null) | |
{ | |
throw new KeyNotFoundException("找不到 __Server 資料"); | |
} | |
ret.connectHost = connectHost.ToString(); | |
object userFriendlyName = queryObject["UserFriendlyName"]; | |
if (userFriendlyName == null) | |
{ | |
throw new KeyNotFoundException("找不到 UserFriendlyName 資料"); | |
} | |
ret.makeModel = convertWMIDataToString(userFriendlyName); | |
object serialNumber = queryObject["SerialNumberID"]; | |
if (serialNumber == null) | |
{ | |
throw new KeyNotFoundException("找不到 SerialNumberID 資料"); | |
} | |
ret.serialNumber = convertWMIDataToString(serialNumber); | |
return ret; | |
} | |
} | |
public static string outputWMIResult() | |
{ | |
StringBuilder ret = new StringBuilder(); | |
ret.AppendLine("Host SN"); | |
ret.AppendLine($"Serial Number : {queryWMIHost()}").AppendLine(); | |
MonitorInformation monitorInformation = queryWMIMonitor(); | |
ret.AppendLine("Monitor SN"); | |
ret.AppendLine($"Connect To : {monitorInformation.connectHost}"); | |
ret.AppendLine($"Make Model : {monitorInformation.makeModel}"); | |
ret.Append($"Serial Number : {monitorInformation.serialNumber}"); | |
return ret.ToString(); | |
} | |
public static void Main() | |
{ | |
Console.Title = "查詢主機及螢幕序號"; | |
try | |
{ | |
string output = outputWMIResult(); | |
Console.WriteLine(output); | |
File.WriteAllText(OUTPUT_FILE, output, Encoding.UTF8); | |
using (Process.Start(OUTPUT_FILE)) | |
{ | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.Error.WriteLine($"發生例外狀況 : {ex.Message}"); | |
} | |
Console.Write("請按任意鍵繼續 ..."); | |
Console.ReadKey(true); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment