NewLife/XCoder

添加获取系统信息类
笑笑 authored at 2019-11-21 23:24:12
24317c2
Tree
1 Parent(s) 0089849
Summary: 3 changed files with 207 additions and 36 deletions.
Modified +19 -30
Added +187 -0
Modified +1 -6
Modified +19 -30
diff --git a/XCoderLinux/Tools/FrmSecurity.cs b/XCoderLinux/Tools/FrmSecurity.cs
index 1db4141..662d70c 100644
--- a/XCoderLinux/Tools/FrmSecurity.cs
+++ b/XCoderLinux/Tools/FrmSecurity.cs
@@ -6,6 +6,7 @@ using System.Management;
 using System.Net;
 using System.Net.NetworkInformation;
 using System.Net.Sockets;
+using System.Runtime.InteropServices;
 using System.Security.Cryptography;
 using System.Text;
 using System.Web;
@@ -13,6 +14,7 @@ using Atk;
 using Gtk;
 using NewLife;
 using NewLife.Collections;
+using NewLife.Log;
 using NewLife.Reflection;
 using NewLife.Security;
 using XCoder.Util;
@@ -413,37 +415,23 @@ namespace XCoder.Tools
         {
             var sb = Pool.StringBuilder.Get();
 
-            var macs = GetMacs().ToList();
-            if (macs.Count > 0) sb.AppendFormat("MAC:\t{0}\r\n", macs.Join(",", x => x.ToHex("-")));
+            // Linux 获取macs有问题
+            //var macs = GetMacs().ToList();
+            //if (macs.Count > 0) sb.AppendFormat("MAC:\t{0}\r\n", macs.Join(",", x => x.ToHex("-")));
+
+            var machineInfo = new MachineInfo();
+            Console.WriteLine("MachineInfo");
+            sb.AppendLine($"系统名称:{machineInfo.OSName}");
+            sb.AppendLine($"系统版本:{Environment.OSVersion.VersionString}");
+            sb.AppendLine($"是否64位操作系统:{Environment.Is64BitOperatingSystem}");
+            sb.AppendLine($"系统架构:{RuntimeInformation.OSArchitecture}");
+            sb.AppendLine($"进程架构:{RuntimeInformation.ProcessArchitecture}");
+            sb.AppendLine($"处理器:{machineInfo.Processor}");
+            sb.AppendLine($"处理器核心数:{Environment.ProcessorCount}");
+            Console.WriteLine(machineInfo.AvailableMemory);
+            sb.AppendLine($"总内存:{machineInfo.Memory}");
+            sb.AppendLine($"可用内存:{machineInfo.AvailableMemory} {(machineInfo.AvailableMemory.Contains("k") ? "" : "kb")}");
 
-            var processor = GetInfo("Win32_Processor", "Name");
-            /*if (!processor.IsNullOrEmpty())*/ sb.AppendFormat("Processor:\t{0}\t(Win32_Processor.Name)\r\n", processor);
-
-            var cpuID = GetInfo("Win32_Processor", "ProcessorId");
-            /*if (!cpuID.IsNullOrEmpty())*/ sb.AppendFormat("ProcessorId:\t{0}\t(Win32_Processor.ProcessorId)\r\n", cpuID);
-
-            var uuid = GetInfo("Win32_ComputerSystemProduct", "UUID");
-            /*if (!uuid.IsNullOrEmpty())*/ sb.AppendFormat("UUID:\t{0}\t(Win32_ComputerSystemProduct.UUID)\r\n", uuid);
-
-            var id = GetInfo("Win32_ComputerSystemProduct", "IdentifyingNumber");
-            /*if (!id.IsNullOrEmpty())*/ sb.AppendFormat("IdentifyingNumber:\t{0}\t(Win32_ComputerSystemProduct.IdentifyingNumber)\r\n", id);
-
-            var bios = GetInfo("Win32_BIOS", "SerialNumber");
-            /*if (!bios.IsNullOrEmpty())*/ sb.AppendFormat("BIOS:\t{0}\t(Win32_BIOS.SerialNumber)\r\n", bios);
-
-            var baseBoard = GetInfo("Win32_BaseBoard", "SerialNumber");
-            /*if (!baseBoard.IsNullOrEmpty())*/ sb.AppendFormat("BaseBoard:\t{0}\t(Win32_BaseBoard.SerialNumber)\r\n", baseBoard);
-
-            var serialNumber = GetInfo("Win32_DiskDrive", "SerialNumber");
-            /*if (!serialNumber.IsNullOrEmpty())*/ sb.AppendFormat("DiskSerial:\t{0}\t(Win32_DiskDrive.SerialNumber)\r\n", serialNumber);
-
-            //var reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Cryptography");
-            //if (reg != null)
-            //{
-            //    var guid = reg.GetValue("MachineGuid") + "";
-            //    /*if (!guid.IsNullOrEmpty())*/ sb.AppendFormat("MachineGuid:\t{0}\t(SOFTWARE\\Microsoft\\Cryptography)\r\n", guid);
-            //}
-            
             rtResult.Buffer.Text = sb.Put(true);
         }
 
@@ -468,6 +456,7 @@ namespace XCoder.Tools
         #endregion
 
         #region WMI辅助
+
         /// <summary>获取WMI信息</summary>
         /// <param name="path"></param>
         /// <param name="property"></param>
Added +187 -0
diff --git a/XCoderLinux/Tools/MachineInfo.cs b/XCoderLinux/Tools/MachineInfo.cs
new file mode 100644
index 0000000..29ab787
--- /dev/null
+++ b/XCoderLinux/Tools/MachineInfo.cs
@@ -0,0 +1,187 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Management;
+using System.Runtime.InteropServices;
+using System.Text;
+using System.Threading.Tasks;
+using NewLife;
+using NewLife.Log;
+
+namespace XCoder.Tools
+{
+    public class MachineInfo
+    {
+        #region 属性
+        /// <summary>系统名称</summary>
+        public String OSName { get; private set; }
+
+        /// <summary>系统版本</summary>
+        public String OSVersion { get; private set; }
+
+        /// <summary>处理器序列号</summary>
+        public String Processor { get; private set; }
+
+        /// <summary>处理器序列号</summary>
+        public String CpuID { get; private set; }
+
+        /// <summary>唯一标识</summary>
+        public String UUID { get; private set; }
+
+        /// <summary>机器标识</summary>
+        public String Guid { get; private set; }
+
+        /// <summary>内存总量</summary>
+        public String Memory { get; private set; }
+
+#if __WIN__
+        private ComputerInfo _cinfo;
+        /// <summary>可用内存</summary>
+        public UInt64 AvailableMemory => _cinfo.AvailablePhysicalMemory;
+
+        private PerformanceCounter _cpuCounter;
+        /// <summary>CPU占用率</summary>
+        public Single CpuRate => _cpuCounter == null ? 0 : (_cpuCounter.NextValue() / 100);
+#else
+        /// <summary>可用内存</summary>
+        public String AvailableMemory { get; private set; }
+
+        /// <summary>CPU占用率</summary>
+        public Single CpuRate { get; private set; }
+#endif
+
+        /// <summary>温度</summary>
+        public Double Temperature { get; }
+        #endregion
+
+        #region 构造
+        /// <summary>实例化机器信息</summary>
+        public MachineInfo()
+        {
+            Refresh();
+        }
+        #endregion
+
+        #region 方法
+        /// <summary>刷新</summary>
+        public void Refresh()
+        {
+
+            OSName = RuntimeInformation.OSDescription;
+            OSVersion = Environment.OSVersion.ToString();
+
+            if (Runtime.Windows)
+            {
+                Processor = GetInfo("Win32_Processor", "Name");
+                CpuID = GetInfo("Win32_Processor", "ProcessorId");
+                UUID = GetInfo("Win32_ComputerSystemProduct", "UUID");
+                OSName = GetInfo("Win32_OperatingSystem", "Caption");
+                AvailableMemory = GetInfo("Win32_OperatingSystem", "FreePhysicalMemory");
+                //SerialNumber
+                Memory = GetInfo("Win32_OperatingSystem", "TotalVisibleMemorySize");
+            }
+            else if (Runtime.Linux)
+            {
+                OSName = GetOSName();
+                //Memory = GetMemoryInfo();
+                SetMemoryInfo();
+                Processor = GetCpuInfo();
+            }
+        }
+        #endregion
+
+        #region WMI辅助
+        /// <summary>获取WMI信息</summary>
+        /// <param name="path"></param>
+        /// <param name="property"></param>
+        /// <returns></returns>
+        public static String GetInfo(String path, String property)
+        {
+            // Linux Mono不支持WMI
+            if (Runtime.Mono) return "";
+
+            var bbs = new List<String>();
+            try
+            {
+                var wql = String.Format("Select {0} From {1}", property, path);
+                var cimobject = new ManagementObjectSearcher(wql);
+                var moc = cimobject.Get();
+                foreach (var mo in moc)
+                {
+                    if (mo != null &&
+                        mo.Properties != null &&
+                        mo.Properties[property] != null &&
+                        mo.Properties[property].Value != null)
+                        bbs.Add(mo.Properties[property].Value.ToString());
+                }
+            }
+            catch (Exception ex)
+            {
+                //XTrace.WriteException(ex);
+                XTrace.WriteLine("WMI.GetInfo({0})失败!{1}", path, ex.Message);
+                return "";
+            }
+
+            bbs.Sort();
+
+            return bbs.Distinct().Join();
+        }
+        #endregion
+
+        #region Linux辅助
+        /*
+         * https://blog.csdn.net/u014518337/article/details/86291984
+         */
+
+        public static String GetOSName()
+        {
+            const String cpuFilePath = "/etc/issue";
+            var s = File.ReadAllText(cpuFilePath);
+            return s.Trim();
+        }
+        public static String GetCpuInfo()
+        {
+            const String cpuFilePath = "/proc/cpuinfo";
+            var s = File.ReadAllText(cpuFilePath);
+            var lines = s.Split(new[] { '\n' });
+            s = String.Empty;
+
+            foreach (var item in lines)
+            {
+                if (item.StartsWith("model name"))
+                {
+                    var temp = item.Split(new[] { ':' });
+                    s = temp[1].Trim();
+                    break;
+                }
+            }
+            return s;
+        }
+
+        public void SetMemoryInfo()
+        {
+            const String cpuFilePath = "/proc/meminfo";
+            var s = File.ReadAllText(cpuFilePath);
+            var lines = s.Split(new[] { '\n' });
+            s = String.Empty;
+
+            foreach (var item in lines)
+            {
+                if (item.StartsWith("MemTotal"))
+                {
+                    var temp = item.Split(new[] { ':' });
+                    Memory = temp[1].Trim();
+                    //break;
+                }
+                else if (item.StartsWith("MemFree"))
+                {
+                    var temp = item.Split(new[] { ':' });
+                    AvailableMemory = temp[1].Trim();
+                    return; // MemFree位于MemTotal后,因此到这里之后直接返回
+                }
+            }
+        }
+        #endregion
+    }
+}
Modified +1 -6
diff --git a/XCoderLinux/XCoderLinux.csproj b/XCoderLinux/XCoderLinux.csproj
index 0ef3934..c5135d8 100644
--- a/XCoderLinux/XCoderLinux.csproj
+++ b/XCoderLinux/XCoderLinux.csproj
@@ -47,6 +47,7 @@
     <PackageReference Include="NewLife.Core" Version="8.4.2019.1109" />
     <PackageReference Include="NewLife.XCode" Version="9.14.2019.1109" />
     <PackageReference Include="System.IO.Ports" Version="4.6.0" />
+    <PackageReference Include="System.Management" Version="4.6.0" />
   </ItemGroup>
 
   <ItemGroup>
@@ -55,12 +56,6 @@
   </ItemGroup>
 
   <ItemGroup>
-    <Reference Include="System.Management">
-      <HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Management.dll</HintPath>
-    </Reference>
-  </ItemGroup>
-
-  <ItemGroup>
     <Compile Update="Tools\FrmSecurity.cs" />
     <Compile Update="Tools\FrmSecurity.Designer.cs">
       <DependentUpon>FrmSecurity.cs</DependentUpon>