Reputation: 8031
I have the following struct;
static Memory memory;
public struct Memory
{
public string Name;
public string Manufacturer;
public string MemoryType ;
public string Speed;
public string DeviceLocator;
public string Capacity;
public string OtherInfo;
};
I can obtain everything but the following:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMemory");
ManagementObjectCollection myobject = searcher.Get();
foreach (ManagementObject item in myobject)
{
memory.OtherInfo = item["OtherIdentifyingInfo"].ToString();
}
When i try to do the above, i get:
Object reference not set to an instance of an object.
Do i have to somehow create an instance of item["OtherIdentifyingInfo"]
?
Upvotes: 1
Views: 1582
Reputation: 43616
A simple null
check will fix the error, but it looks like Win32_PhysicalMemory
does not use OtherIdentifyingInfo
memory = new Memory();
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMemory");
ManagementObjectCollection myobject = searcher.Get();
foreach (ManagementObject item in myobject)
{
if (item["OtherIdentifyingInfo"] != null)
memory.OtherInfo = item["OtherIdentifyingInfo"].ToString();
}
Upvotes: 1
Reputation: 8628
OtherIdentifyingInfo
Data type: string Access type: Read-only Additional data, beyond asset tag information, that can be used to identify a physical element. One example is bar code data associated with an element that also has an asset tag. If only bar code data is available and unique or able to be used as an element key, this property is be NULL and the bar code data is used as the class key in the tag property. This property is inherited from CIM_PhysicalElement.
You need to Check if the value is Null before you pass to string.
Upvotes: 3