我需要知道如何获得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();
答案 0 :(得分: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();
}