Device Reports Loaded Media When No Media Is Present - c#

I have 2 USB SD Card readers. One has a card in it, the other does not. The system reports correctly within the Disk Management control panel:
Disk 1 Removable 7.40 GB Read Only
(G:) 7.4 GB RAW
Disk 2 Removable (E:) No Media
(No Volume Info)
However, when I use ManagementObjectSearcher to search for the drives and query their properties, I get:
\.\PHYSICALDRIVE1: Media Loaded
\.\PHYSICALDRIVE2: Media Loaded
This is obviously Incorrect, and I'm wondering why. Here's the code I'm using
using (ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType=\"USB\"")) {
using (ManagementObjectCollection moc = mos.Get()) {
int i = 0;
foreach (ManagementObject mo in moc) {
using (mo) {
Console.WriteLine($"{mo.Properties["DeviceID"].Value as string}: Media Loaded? {mo.Properties["MediaLoaded"].Value}");
}
_operationFragment.ProgressValue = (i + 1 / moc.Count);
i++;
}
}

Related

How can I find out what graphic card a screen is connected to in C#?

I need to programatically find out what graphic card a screen is connected to using C#.
This information is visible in DxDiag, speccy and even in windows advanced display settings view:
But how can I do this in C# myself?
The following is code by #NielW in C# detect which graphics card drives video
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);
}
}
}
There is a long talk in the link because they have issues getting multiples graphic cards' names. But this code should work and it shows all names of available graphic cards.

Incorrect hard drive serial number

I am currently developing a program that will allow me to detect the serial number of all hard drives in a computer and display them as a barcode.
I have it working on my current machine (Windows 10) and it gets the correct serial numbers (same as what's on the hard drive label) but when I try and use it on a different machine (Windows 7) it just outputs a long string of numbers.
The program is able to detect and output the serial number on my usb fine.
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject wmi_HD in searcher.Get())
{
HardDrive hd = new HardDrive();
hd.Model = wmi_HD["Model"].ToString();
hd.SerialNum = Convert.ToString(wmi_HD.GetPropertyValue("SerialNumber"));
hdCollection.Add(hd);
}
This is the current code I am using but the serial number it outputs is: 5635454d4338414e202020202020202020202020
I have tried Win32_LogicalDrive and Win32_Volume but they output the same string.
I have also tried this code snippet:
ManagementClass driveClass = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection drives = driveClass.GetInstances();
foreach (ManagementObject drive in drives)
{
HardDrive hd = new HardDrive();
hd.Model = drive["Name"].ToString();
//hd.SerialNum = drive.GetPropertyValue("SerialNumber").ToString();
hd.SerialNum = drive["SerialNumber"].ToString();
}
But this also does not work on the Windows 7 machine.
How could I fix my problem?

How to detect Cameras attached to my machine using C#

I am able to know the sound devices and USB Devices attached to my PC but not getting any way to find Cameras attached to my machine.
Used below code to get Sound Devices
Console.WriteLine("Win32 SoundDevices\r\n===============================");
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * from Win32_SoundDevice");
foreach (ManagementObject soundDevice in searcher.Get())
{
//Console.WriteLine("Device found: {0}\n", soundDevice.ToString());
Console.WriteLine("Device found: {0}\n", soundDevice.GetPropertyValue("ProductName"));
}
Console.WriteLine("Search complete.");
I wrote a solution that worked well for me. This method found two USB cameras and an integrated camera on my laptop.
private static async Task ListAllCamerasAsync()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM win32_PNPEntity WHERE PnPClass='Camera'");
var collection = await Task.Run(() => { return searcher.Get(); });
var cameraIndex = 0;
foreach (ManagementObject cameraDevice in collection)
{
Console.WriteLine($"Camera {cameraIndex}: {cameraDevice["Name"]}");
cameraIndex++;
}
}
This tool will prove helpful:
http://www.microsoft.com/en-us/download/details.aspx?id=8572
I'm fairly certain there's no equivalent string you can send to ManagementObjectSearcher() for webcams specifically. There is the "Win32_USBControllerDevice" which you can then determine if it is a webcam or not.
A better solution altogether is to take advantage of DirectShow.NET

Detecting input and output audio devices separately in C#

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?

C# detect which graphics card drives video

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;
}
}
}

Categories