//1. 取得设备硬盘的物理序列号 仅支持windows桌面程序(unity用不了) public static List<string> GetSerialNumber() { List<string> serial_number_list = new List<string>(); try { //Win32_PhysicalMedia或者 Win32_DiskDrive ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia"); foreach (ManagementObject info in searcher.Get()) { //info.Properties["SerialNumber"].ToString().Trim(); string serial_number = info["SerialNumber"].ToString().Trim(); serial_number_list.Add(serial_number); } } catch (Exception e) { Console.WriteLine(e.Message); } return serial_number_list; } //2. 取得设备硬盘的物理序列号 unity可用 public static string GetSerialNumberByCmd() { string strInput = "wmic diskdrive get SerialNumber"; Process p = new Process(); //设置要启动的应用程序 p.StartInfo.FileName = "cmd.exe"; //是否使用操作系统shell启动 p.StartInfo.UseShellExecute = false; // 接受来自调用程序的输入信息 p.StartInfo.RedirectStandardInput = true; //输出信息 p.StartInfo.RedirectStandardOutput = true; // 输出错误 p.StartInfo.RedirectStandardError = true; //不显示程序窗口 p.StartInfo.CreateNoWindow = true; //启动程序 p.Start(); Thread.Sleep(1000); //向cmd窗口发送输入信息 p.StandardInput.WriteLine(strInput + "&exit"); p.StandardInput.AutoFlush = true; //获取输出信息 string strOuput = p.StandardOutput.ReadToEnd(); //等待程序执行完退出进程 p.WaitForExit(); p.Close(); List<string> serialNumbers = new List<string>(); string[] temps= strOuput.Split(new char[] { '\r', '\n' },StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < temps.Length; i++) { serialNumbers.Add(temps[i].Trim()); } if (serialNumbers.Count>4) { return serialNumbers[4]; } else { return ""; } }
标签:string,C#,number,new,StartInfo,序列号,true,serial,硬盘 From: https://www.cnblogs.com/dj1232090/p/16783936.html