I would like to know how to add item to the dictionary from the first foreach loop and the second foreach loop at the same time..
For example - the first loop adds the first contents to the item and starts the second loop which I want it to add the first item in the loop and then starts the outside loop without reading the second item. Do the same and add the second item from the second loop.
Sorry if the question is confusing..Weak english.
List<object> items = new List<object>();
DeviceSettings deviceSettings = new DeviceSettings();
List<object> deviceName = deviceSettings.GetMonitorFriendlyName();
using (ManagementObjectCollection moc = searcher.Get())
{
foreach (ManagementObject mo in moc)
{
Dictionary<string, object> item = new Dictionary<string, object>();
ConnectedMonitor_Number = searcher.Get().Count;
item.Add("DefaultMonitorLength", DefaultMonitor_Width);
item.Add("DefaultMonitorHeight", DefaultMonitor_Height);
item.Add("ConnectedMonitor_Numb", Convert.ToString(ConnectedMonitor_Number));
item.Add("Caption", Convert.ToString(mo["Caption"]));
item.Add("Name", Convert.ToString(mo["Name"]));
item.Add("Description", Convert.ToString(mo["Description"]));
item.Add("DeviceID", Convert.ToString(mo["DeviceID"]));
item.Add("Manufacturer", Convert.ToString(mo["Manufacturer"]));
string[] HardwareID = (string[])mo["HardwareID"];
item.Add("HardwareID", string.Join(";", HardwareID));
item.Add("Status", Convert.ToString(mo["Status"]));
foreach (Dictionary<string, string> dm in deviceName)
{
item["monitorname"] = Convert.ToString(dm["monitorname"]);
}
items.Add(item);
}
}
---This is the devicesettings.cs ---
public static string MonitorFriendlyName(LUID adapterId, uint targetId)
{
DISPLAYCONFIG_TARGET_DEVICE_NAME deviceName = new DISPLAYCONFIG_TARGET_DEVICE_NAME();
deviceName.header.size = (uint)Marshal.SizeOf(typeof(DISPLAYCONFIG_TARGET_DEVICE_NAME));
deviceName.header.adapterId = adapterId;
deviceName.header.id = targetId;
deviceName.header.type = DISPLAYCONFIG_DEVICE_INFO_TYPE.DISPLAYCONFIG_DEVICE_INFO_GET_TARGET_NAME;
int error = DisplayConfigGetDeviceInfo(ref deviceName);
if (error != ERROR_SUCCESS)
throw new Win32Exception(error);
return deviceName.monitorFriendlyDeviceName;
}
public List<object> GetMonitorFriendlyName()
{
try
{
List<object> items = new List<object>();
uint PathCount, ModeCount;
int error = GetDisplayConfigBufferSizes(QUERY_DEVICE_CONFIG_FLAGS.QDC_ONLY_ACTIVE_PATHS,
out PathCount, out ModeCount);
if (error != ERROR_SUCCESS)
{
throw new Win32Exception(error);
}
DISPLAYCONFIG_PATH_INFO[] DisplayPaths = new DISPLAYCONFIG_PATH_INFO[PathCount];
DISPLAYCONFIG_MODE_INFO[] DisplayModes = new DISPLAYCONFIG_MODE_INFO[ModeCount];
error = QueryDisplayConfig(QUERY_DEVICE_CONFIG_FLAGS.QDC_ONLY_ACTIVE_PATHS,
ref PathCount, DisplayPaths, ref ModeCount, DisplayModes, IntPtr.Zero);
for (int i = 1; i < ModeCount; i++)
{
if (DisplayModes[i].infoType == DISPLAYCONFIG_MODE_INFO_TYPE.DISPLAYCONFIG_MODE_INFO_TYPE_TARGET)
{
Dictionary<string, string> item = new Dictionary<string, string>();
item["MonitorName"] = (MonitorFriendlyName(DisplayModes[i].adapterId, DisplayModes[i].id));
items.Add(item);
}
}
return items;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}