2012-07-10 48 views
1

我需要知道如何获得RAM(物理内存)序列号。我正在使用C#,并且使用WMI获取硬件信息,但在另一台计算机上序列号返回null。我想知道如何在任何计算机上(而不是WMI)获得它,并且如果没有其他方式,我可以使用C++编写它,并在此函数和我的应用程序之间建立连接?我的代码: WqlObjectQuery Memory3_objectQuery = new WqlObjectQuery(“Select * from Win32_PhysicalMemory”); ManagementObjectSearcher Memory3_Searcher = new ManagementObjectSearcher(Memory3_objectQuery); 的foreach(MO2的ManagementObject在Memory3_Searcher.Get()){ 我如何获得内存序列号?

cmd.Parameters.Clear(); 
cmd.Parameters.AddWithValue("@Component_Type", "RAM");  


try 
{ 
    Model = MO2["Model"].ToString(); 
    if (Model != null) 
    { 
     cmd.Parameters.AddWithValue("@Model", Model); 
    } 
    else { } 
} 
catch (NullReferenceException) { } 

try 
{ 
    Capacity = MO2["Capacity"].ToString(); 
    if (Capacity != null) 
    { 
     cmd.Parameters.AddWithValue("@Capacity", Capacity); 
    } 
    else { } 
} 
catch (NullReferenceException) 
{ } 
try 
{ 
    Serial = MO2["SerialNumber"].ToString(); 
    if (Serial != null) 
    { 
     cmd.Parameters.AddWithValue("@SerialNumber", Serial); 
    } 
    else { } 
} 
catch (NullReferenceException) 
{ 
} 
try 
{ 
    Manufacturer = MO2["Manufacturer"].ToString(); 
    if (Manufacturer != null) 
    { 
     cmd.Parameters.AddWithValue("@Manufacturer", Manufacturer); 
    } 
    else { } 
} 

catch (NullReferenceException) 
{ 
} 


// Console.WriteLine("Serial Number Bank" + count + ": " + s); 
try 
{ 
    s = MO2["MemoryType"].ToString(); 
    if (s.Equals("21")) 
    { 
     s = "DDr2"; 
     cmd.Parameters.AddWithValue("@Memory_Type", s); 
    } 
    else if (s.Equals("20")) 
    { 
     s = "DDr"; 
     cmd.Parameters.AddWithValue("@Memory_Type", s); 
    } 
    else if (s.Equals("17")) 
    { 
     s = "SDRAM"; 
     cmd.Parameters.AddWithValue("@Memory_Type", s); 
    } 
} 
catch (NullReferenceException) { } 
cmd.Parameters.AddWithValue("@Computer_Name", myHost); 
cmd.ExecuteNonQuery(); 
cmd.Parameters.Clear(); 
+1

这是高度依赖于主板制造商。除(某些)服务器主板外,你不会得到这个。 – leppie 2012-07-10 10:07:35

+0

获取所有硬件信息请访问以下链接,非常链接:http://www.codescratcher.com/windows-forms/get-computer-hardware-information-using-c/ – Hamed 2016-08-24 14:00:04

回答

0
//SEE http://licensetoolkit.com/blog/hardware-identification-using-ram/ 
//using System.Management; 
public static void OutputRamInformation() 
{ 
    // Create our WMI searcher to do our query 
    ManagementObjectSearcher searcher = new 
      ManagementObjectSearcher("select * from Win32_PhysicalMemory"); 
    // Now loop through all the item found with the query 
    int ram = 1; 
    foreach (ManagementObject obj in searcher.Get()) 
    { 
     Console.WriteLine(String.Format("RAM #{0}:" , ram)); 
     // Now step through each of the properties and output their values 
     foreach (PropertyData property in obj.Properties) 
     { 
      if (property.Value != null) 
      { 
       Console.WriteLine(property.Name + " = " + 
        property.Value.ToString()); 
      } 
     } 
     Console.WriteLine("---------------------------------"); 
     // Increment our ram chip count 
     ram++; 
    } 
    // Wait for a keypress before continuting 
    Console.WriteLine("Press any key to continue..."); 
    Console.ReadKey(); 
}