First of all, apologies if this doesn't make sense, and/or it has already been asked (although searching didn't find anything).
I have an application which sets default printers for our end users but I would like to expand it by also making it able to install printers from a remote machine as well.
What I need to do is on Form_Load populate a Combo Box with all network shares from the print server.
I am shooting in the dark and am wondering if anyone can shed some light.
I believe this works.
This isnt my code originally but I cant remember where it came from.
using System.Management;
private void btnGetPrinters_Click(object sender, EventArgs e) {
// Use the ObjectQuery to get the list of configured printers.
ObjectQuery oquery = new ObjectQuery("SELECT * FROM Win32_Printer");
ManagementObjectSearcher mosearcher = new ManagementObjectSearcher(oquery);
ManagementObjectCollection moc = mosearcher.Get();
foreach (ManagementObject mo in moc)
{
PropertyDataCollection pdc = mo.Properties;
foreach (System.Management.PropertyData pd in pdc)
{
if ((bool)mo["Network"])
{
cmbPrinters.Items.Add(mo[pd.Name]);
}
}
}
}
I have a C# project where I am trying to deploy a protection mechanism by registering a combination of the Hardware IDs.
I am using the ManagementObjectSearcher Class for the same. Here are some of the commands:
ManagementObjectSearcher cpuget = new ManagementObjectSearcher("Select * From Win32_processor");
ManagementObjectSearcher mainboardget = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
ManagementObjectSearcher biosget = new ManagementObjectSearcher("Select * From Win32_BIOS");
For fetching the IDs I have:
foreach (ManagementObject mo in cpuList)
{
cpuid = mo["ProcessorID"].ToString();
}
foreach (ManagementObject mo in mainboardlist)
{
mbid = mo["SerialNumber"].ToString();
}
This has been working fine. However, *in some machines(I tested on 10 PCs and two were defaulters)* the error message showed up.
Reference not set to Instance of an Object
Why so? Please Help.
The most difficult problems have the silliest of Answers. Period.
All I had to do was Check for a null component in the management object class.
foreach (ManagementObject mo in cpuget.Get())
{
if (mo["ProcessorID"] != null)
cpuid = mo.GetPropertyValue("ProcessorID").ToString();
}
The similar would be for mbid.
What a dufus? :|
P.S.: #L.B suggested this. More power to him.
I am searching for my USB to Serial converter. I have two objectives:
To get the USB Port Number
To get the Provider
Here under is the screenshot from my Device Manager
Now my motto is to recognize this USB to Serial Bridge which is provided by ATEN, However I am not specifically looking for the ATEN thing, just I need to know how to query ManagementObject
Because if I look for Caption for some systems it is ATEN USB to Serial Bridge and for some systems it is USB-to-Serial Comm. Port, and maybe for some other it will become something else. What never changes is the Provider - "ATEN"
Following is my code:
Now I have did something here under to get/extract data from **CAPTION**
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE Caption like '%ATEN USB to Serial Bridge%'"))
{
foreach (ManagementObject queryObj in searcher.Get())
{
foreach (PropertyData prop in queryObj.Properties)
{
listBox1.Items.Add(String.Format("{0}: {1}", prop.Name, prop.Value));
}
string s = queryObj["Caption"].ToString();
// Here under i play with the string `s` to get the `CCOM21`
int start = s.IndexOf("(");
int end = s.IndexOf(")");
result = s.Substring(start, end - start);
result = result.Replace("(", "");
list.Items.Add(result);
}
}
I know its not the right way to do but how I can get the COM21 port and Provider - ATEN by query ManagementObject. Or is there any other direct way to do this.
I am trying to search for all available sound input and outut devices and adding their names into a ComboBox.
I am using this code (found here at stackoverflow in a similiar question):
ManagementObjectSearcher mo = new ManagementObjectSearcher("select * from Win32_SoundDevice");
foreach (ManagementObject soundDevice in mo.Get())
{
string deviceId = soundDevice.GetPropertyValue("DeviceId").ToString();
string name = soundDevice.GetPropertyValue("Name").ToString();
comboBox1.Items.Add(name);
}
But this seems to select all available devices, I need to separate input devices and output devices. Is there a simple way of editing this code, or is there a more sophisticated method needed?
My C# application sits on the embedded box which has Intel motherboard and graphics chipset. ATI graphics card is put on to PCI express. Generally graphics card drives the video, but if ATI card fails then the video comes out from graphics chipset.
I have to detect the failure of ATI graphics card for diagnostic purposes.
Any ideas/sample code on how to do this.
Thanks in advance
Raju
This should hopefully get you started.
Add a reference to System.Management, then you can do this:
ManagementObjectSearcher searcher
= new ManagementObjectSearcher("SELECT * FROM Win32_DisplayConfiguration");
string graphicsCard = string.Empty;
foreach (ManagementObject mo in searcher.Get())
{
foreach (PropertyData property in mo.Properties)
{
if (property.Name == "Description")
{
graphicsCard = property.Value.ToString();
}
}
}
In my case, graphicsCard is equal to
NVIDIA GeForce 8400 GS (Microsoft
Corporation - WDDM v1.1)
I'm not a fan of how the selected answer only returns the first video controller. Also, there's no need to loop over all the properties. Just get the ones you need. If CurrentBitsPerPixel is not null, then you're looking at one of the active controllers. I'm using Win32_VideoController as suggested by #bairog, instead of the deprecated Win32_DisplayConfiguration.
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("SELECT * FROM Win32_VideoController");
foreach (ManagementObject mo in searcher.Get())
{
PropertyData currentBitsPerPixel = mo.Properties["CurrentBitsPerPixel"];
PropertyData description = mo.Properties["Description"];
if (currentBitsPerPixel != null && description != null)
{
if (currentBitsPerPixel.Value != null)
System.Console.WriteLine(description.Value);
}
}
My machine has 3 video controllers. The first one is not active (ShoreTel). The second one is active, but is not the video card (Desktop Authority). The third one is my NVidia. This code will print out both the DA controller and the NVidia controller.
Promoted answer works only for single video card system. When I have ATI and Nvidia cards - WMI query returns ATI even if that monitor is plugged into Nvidia card, dxdiag shows Nvidia and games runs on that card (usage).
The only way I could determine right video card was using SlimDX to create DX device and examine what card it used. However that .dll weights over 3Mb.
var graphicsCardName = new Direct3D().Adapters[0].Details.Description;
Your question isn't entirely clear, so I'm not sure if the follwing idea will help or not.
Perhaps something very simple would suffice:
If the two graphics cards run different resolutions check the monitor resolution using:
System.Windows.Forms.SystemInformation.PrimaryMonitorSize
Similarly, if one card supports more than one monitor, check the number of monitors using SystemInformation.MonitorCount.
I tried all the approaches in this question but none gives me a correct answer. However I found it possible to get your current using the Win32_DisplayControllerConfiguration class. Although according to MSDN this class is obsolete, it's the only one returning a correct answer:
using System;
using System.Management;
using System.Windows.Forms;
namespace WMISample
{
public class MyWMIQuery
{
public static void Main()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_DisplayControllerConfiguration");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("----------------------------------- ");
Console.WriteLine("Win32_DisplayControllerConfiguration instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("Name: {0}", queryObj["Name"]);
}
}
catch (ManagementException e)
{
MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
}
}
}
}
(Code generated by WMI Code Creator, a great tool if you are messing with WMI.)
This gives GeForce GTX 1080 on my Windows 10 (RS2) + Intel(R) HD Graphics 4600 + NVIDIA GeForce GTX 1080 system.
Sometimes I need to switch between the Nvidia GPU and onboard GPU. To know which is connected to the monitor, I use the property MinRefreshRate. It works reliably for me, not CurrentBitsPerPixel.
public static void UpdateActiveGpu()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_VideoController");
foreach (ManagementObject mo in searcher.Get())
{
PropertyData minRefreshRate = mo.Properties["MinRefreshRate"];
PropertyData description = mo.Properties["Description"];
if (minRefreshRate != null && description != null && minRefreshRate.Value != null)
{
Global.Instance.activeGpu = description.Value.ToString();
break;
}
}
}