NewLife/X

机器信息获取磁盘id时,只要固定磁盘,剔除U盘和移动硬盘等
大石头 authored at 2023-03-31 18:58:55
a6261c6
Tree
1 Parent(s) 2718f06
Summary: 4 changed files with 25 additions and 13 deletions.
Modified +9 -11
Modified +10 -0
Modified +5 -2
Modified +1 -0
Modified +9 -11
diff --git a/NewLife.Core/Common/MachineInfo.cs b/NewLife.Core/Common/MachineInfo.cs
index 30c4fa7..3014ecd 100644
--- a/NewLife.Core/Common/MachineInfo.cs
+++ b/NewLife.Core/Common/MachineInfo.cs
@@ -311,13 +311,13 @@ public class MachineInfo
         //CpuID = GetInfo("Win32_Processor", "ProcessorId");
         //var uuid = GetInfo("Win32_ComputerSystemProduct", "UUID");
         //Product = GetInfo("Win32_ComputerSystemProduct", "Name");
-        DiskID = GetInfo("Win32_DiskDrive", "SerialNumber");
+        DiskID = GetInfo("Win32_DiskDrive where mediatype=\"Fixed hard disk media\"", "SerialNumber");
 
         var sn = GetInfo("Win32_BIOS", "SerialNumber");
         if (!sn.IsNullOrEmpty() && !sn.EqualIgnoreCase("System Serial Number")) Serial = sn;
         Board = GetInfo("Win32_BaseBoard", "SerialNumber");
 #else
-        var disk = ReadWmic("diskdrive", "serialnumber");
+        var disk = ReadWmic("diskdrive where mediatype=\"Fixed hard disk media\"", "serialnumber");
         if (disk != null)
         {
             if (disk.TryGetValue("serialnumber", out str)) DiskID = str?.Trim();
@@ -807,19 +807,17 @@ public class MachineInfo
             {
                 var k = ks[0].Trim();
                 var v = ks[1].Trim();
-                if (dic.TryGetValue(k, out var val))
-                    dic[k] = val + "," + v;
-                else
-                    dic[k] = v;
+                if (!k.IsNullOrEmpty() && !v.IsNullOrEmpty())
+                {
+                    if (dic.TryGetValue(k, out var val))
+                        dic[k] = val + "," + v;
+                    else
+                        dic[k] = v;
+                }
             }
         }
 
         // 排序,避免多个磁盘序列号时,顺序变动
-        //foreach (var item in dic)
-        //{
-        //    if (item.Value.Contains(','))
-        //        dic[item.Key] = item.Value.Split(',').OrderBy(e => e).Join();
-        //}
         var dic2 = new Dictionary<String, String>(StringComparer.OrdinalIgnoreCase);
         foreach (var item in dic)
         {
Modified +10 -0
diff --git a/Test/Program.cs b/Test/Program.cs
index 8766671..891387f 100644
--- a/Test/Program.cs
+++ b/Test/Program.cs
@@ -22,6 +22,7 @@ using NewLife.Remoting;
 using NewLife.Security;
 using NewLife.Serialization;
 using NewLife.Threading;
+using Stardust;
 
 namespace Test
 {
@@ -88,6 +89,7 @@ namespace Test
             }
         }
 
+        static StarClient _client;
         private static void Test1()
         {
             var mi = MachineInfo.GetCurrent();
@@ -95,6 +97,14 @@ namespace Test
 
             var sys = SysConfig.Current;
             XTrace.WriteLine("Name: {0}", sys.Name);
+
+            var client = new StarClient("http://star.newlifex.com:6600")
+            {
+                ProductCode = "Test"
+            };
+            client.Login();
+
+            _client = client;
         }
 
         private static void Test2()
Modified +5 -2
diff --git a/Test/Properties/PublishProfiles/FolderProfile.pubxml b/Test/Properties/PublishProfiles/FolderProfile.pubxml
index 3878cbf..bdebbcb 100644
--- a/Test/Properties/PublishProfiles/FolderProfile.pubxml
+++ b/Test/Properties/PublishProfiles/FolderProfile.pubxml
@@ -7,8 +7,11 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
     <PublishProtocol>FileSystem</PublishProtocol>
     <Configuration>Release</Configuration>
     <Platform>Any CPU</Platform>
-    <TargetFramework>net6.0</TargetFramework>
-    <PublishDir>..\..\Test\publish\</PublishDir>
+    <TargetFramework>net7.0</TargetFramework>
+    <PublishDir>..\Bin\Test\publish\win-x64\</PublishDir>
     <SelfContained>false</SelfContained>
+    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
+    <PublishSingleFile>true</PublishSingleFile>
+    <PublishReadyToRun>false</PublishReadyToRun>
   </PropertyGroup>
 </Project>
\ No newline at end of file
Modified +1 -0
diff --git a/Test/Test.csproj b/Test/Test.csproj
index 9a713f0..c04600c 100644
--- a/Test/Test.csproj
+++ b/Test/Test.csproj
@@ -25,6 +25,7 @@
     <None Remove="Model\**" />
   </ItemGroup>
   <ItemGroup>
+    <PackageReference Include="NewLife.Stardust" Version="2.8.2023.330-beta1303" />
     <PackageReference Include="System.Speech" Version="7.0.0" />
   </ItemGroup>
   <ItemGroup>