Exemplo n.º 1
0
        /// <summary>刷新</summary>
        public void Init()
        {
            var osv = Environment.OSVersion;

            if (OSVersion.IsNullOrEmpty())
            {
                OSVersion = osv.Version + "";
            }
            if (OSName.IsNullOrEmpty())
            {
                OSName = (osv + "").TrimStart("Microsoft").TrimEnd(OSVersion).Trim();
            }
            if (Guid.IsNullOrEmpty())
            {
                Guid = "";
            }

            try
            {
#if __CORE__
                if (Runtime.Windows)
                {
                    LoadWindowsInfo();
                }
                else if (Runtime.Linux)
                {
                    LoadLinuxInfo();
                }
#else
                if (Runtime.Windows)
                {
                    LoadWindowsInfoFx();
                }
                else if (Runtime.Linux)
                {
                    LoadLinuxInfo();
                }
#endif
            }
            catch (Exception ex)
            {
                XTrace.WriteException(ex);
            }

            // window+netcore 不方便读取注册表,随机生成一个guid,借助文件缓存确保其不变
            if (Guid.IsNullOrEmpty())
            {
                Guid = "0-" + System.Guid.NewGuid().ToString();
            }
            if (UUID.IsNullOrEmpty())
            {
                UUID = "0-" + System.Guid.NewGuid().ToString();
            }

            try
            {
                Refresh();
            }
            catch { }
        }
Exemplo n.º 2
0
        /// <summary>刷新</summary>
        public void Init()
        {
            var machine_guid = "";

#if __CORE__
            var osv = Environment.OSVersion;
            if (OSVersion.IsNullOrEmpty())
            {
                OSVersion = osv.Version + "";
            }
            if (OSName.IsNullOrEmpty())
            {
                OSName = (osv + "").TrimStart("Microsoft").TrimEnd(OSVersion).Trim();
            }
            if (Guid.IsNullOrEmpty())
            {
                Guid = "";
            }

            if (Runtime.Windows)
            {
                var str = "";

                var os = ReadWmic("os", "Caption", "Version");
                if (os != null)
                {
                    if (os.TryGetValue("Caption", out str))
                    {
                        OSName = str.TrimStart("Microsoft").Trim();
                    }
                    if (os.TryGetValue("Version", out str))
                    {
                        OSVersion = str;
                    }
                }

                var csproduct = ReadWmic("csproduct", "Name", "UUID");
                if (csproduct != null)
                {
                    if (csproduct.TryGetValue("Name", out str))
                    {
                        Product = str;
                    }
                    if (csproduct.TryGetValue("UUID", out str))
                    {
                        UUID = str;
                    }
                }

                // 不要在刷新里面取CPU负载,因为运行wmic会导致CPU负载很不准确,影响测量
                var cpu = ReadWmic("cpu", "Name", "ProcessorId", "LoadPercentage");
                if (cpu != null)
                {
                    if (cpu.TryGetValue("Name", out str))
                    {
                        Processor = str;
                    }
                    if (cpu.TryGetValue("ProcessorId", out str))
                    {
                        CpuID = str;
                    }
                    if (cpu.TryGetValue("LoadPercentage", out str))
                    {
                        CpuRate = (Single)(str.ToDouble() / 100);
                    }
                }

                // 从注册表读取 MachineGuid
                str = Execute("reg", @"query HKLM\SOFTWARE\Microsoft\Cryptography /v MachineGuid");
                if (!str.IsNullOrEmpty() && str.Contains("REG_SZ"))
                {
                    Guid = str.Substring("REG_SZ", null).Trim();
                }
            }
            // 特别识别Linux发行版
            else if (Runtime.Linux)
            {
                var str = GetLinuxName();
                if (!str.IsNullOrEmpty())
                {
                    OSName = str;
                }

                // 树莓派优先 Model
                var dic = ReadInfo("/proc/cpuinfo");
                if (dic != null)
                {
                    if (dic.TryGetValue("Model", out str) ||
                        dic.TryGetValue("Hardware", out str) ||
                        dic.TryGetValue("cpu model", out str) ||
                        dic.TryGetValue("model name", out str))
                    {
                        Processor = str;
                    }

                    if (dic.TryGetValue("Serial", out str))
                    {
                        CpuID = str;
                    }
                }

                var mid = "/etc/machine-id";
                if (!File.Exists(mid))
                {
                    mid = "/var/lib/dbus/machine-id";
                }
                if (File.Exists(mid))
                {
                    Guid = File.ReadAllText(mid).Trim();
                }

                var file = "/sys/class/dmi/id/product_uuid";
                if (File.Exists(file))
                {
                    UUID = File.ReadAllText(file).Trim();
                }
                file = "/sys/class/dmi/id/product_name";
                if (File.Exists(file))
                {
                    Product = File.ReadAllText(file).Trim();
                }

                var dmi = Execute("dmidecode")?.SplitAsDictionary(":", "\n");
                if (dmi != null)
                {
                    if (dmi.TryGetValue("ID", out str))
                    {
                        CpuID = str.Replace(" ", null);
                    }
                    if (dmi.TryGetValue("UUID", out str))
                    {
                        UUID = str;
                    }
                    if (dmi.TryGetValue("Product Name", out str))
                    {
                        Product = str;
                    }
                    //if (TryFind(dmi, new[] { "Serial Number" }, out str)) Guid = str;
                }
            }
#else
            //// 性能计数器的初始化非常耗时
            //Task.Factory.StartNew(() =>
            //{
            //    try
            //    {
            //        _cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total")
            //        {
            //            MachineName = "."
            //        };
            //        _cpuCounter.NextValue();
            //    }
            //    catch
            //    {
            //        _cpuCounter = null;
            //    }
            //});

            var reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Cryptography");
            if (reg != null)
            {
                machine_guid = reg.GetValue("MachineGuid") + "";
            }
            if (machine_guid.IsNullOrEmpty())
            {
                reg = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
                if (reg != null)
                {
                    machine_guid = reg.GetValue("MachineGuid") + "";
                }
            }

            var ci = new ComputerInfo();
            OSName    = ci.OSFullName.TrimStart("Microsoft").Trim();
            OSVersion = ci.OSVersion;
            Memory    = ci.TotalPhysicalMemory;

            _cinfo = ci;

            Processor = GetInfo("Win32_Processor", "Name");
            CpuID     = GetInfo("Win32_Processor", "ProcessorId");
            UUID      = GetInfo("Win32_ComputerSystemProduct", "UUID");
            Product   = GetInfo("Win32_ComputerSystemProduct", "Name");

            // 读取主板温度,不太准。标准方案是ring0通过IOPort读取CPU温度,太难在基础类库实现
            var str = GetInfo("Win32_TemperatureProbe", "CurrentReading");
            if (!str.IsNullOrEmpty())
            {
                Temperature = str.ToDouble();
            }
            else
            {
                str = GetInfo("MSAcpi_ThermalZoneTemperature", "CurrentTemperature");
                if (!str.IsNullOrEmpty())
                {
                    Temperature = (str.ToDouble() - 2732) / 10.0;
                }
            }
#endif

            if (!machine_guid.IsNullOrEmpty())
            {
                Guid = machine_guid;
            }

            // window+netcore 不方便读取注册表,随机生成一个guid,借助文件缓存确保其不变
            if (Guid.IsNullOrEmpty())
            {
                Guid = "0-" + System.Guid.NewGuid().ToString();
            }
            if (UUID.IsNullOrEmpty())
            {
                UUID = "0-" + System.Guid.NewGuid().ToString();
            }

            Refresh();
        }