2012-07-10 79 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(); 
}