2

我需要知道如何获得 RAM(物理内存)序列号。我正在使用 C# 并使用 WMI 获取硬件信息,但序列号在另一台计算机上返回 null。我想知道如何获取它并在任何计算机(不是 WMI)上工作,如果没有其他方法可以用 C++ 编写它并在此函数和我的应用程序之间建立连接?

这是我的一些代码: WqlObjectQuery Memory3_objectQuery = new WqlObjectQuery("Select * from Win32_PhysicalMemory"); ManagementObjectSearcher Memory3_Searcher = new ManagementObjectSearcher(Memory3_objectQuery); foreach(Memory3_Searcher.Get() 中的 ManagementObject MO2){

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();
4

1 回答 1

1
//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();
}
于 2013-10-25T06:39:51.837 回答