C# 获取电脑SN号

/// <summary>
        /// 获得电脑SN号
        /// </summary>
        public static string GetPcsnString()
        {
            var pcsn = "";
            try
            {
                var search = new ManagementObjectSearcher("SELECT * FROM Win32_BIOS");
                var mobos = search.Get();
                foreach (var temp in mobos)
                {
                    object serial = temp["SerialNumber"]; // ProcessorID if you use Win32_CPU
                    pcsn = serial.ToString();
                    Console.WriteLine(pcsn);

                    if
                    (
                        !string.IsNullOrEmpty(pcsn)
                        && pcsn != "To be filled by O.E.M" //没有找到
                        && !pcsn.Contains("O.E.M")
                        && !pcsn.Contains("OEM")
                        && !pcsn.Contains("Default")
                    )
                    {
                        break;
                    }
                    else
                    {
                        Console.WriteLine("默认值");
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("获取过程发生异常");
                //Debug.WriteLine(e);
                // 无法处理
            }

            return pcsn;
        }
posted @ 2019-12-13 09:07  饮木  阅读(1076)  评论(0编辑  收藏  举报