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?
Related
I try to find a unique ID, which will be the same as the virtual machine and the main machine.
I tried using a monitor ID, bios number, but they can repeat for different end users.
using (ManagementObjectSearcher monitorSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DesktopMonitor"))
{
foreach (ManagementObject monitor in monitorSearcher.Get())
{
String MonitorName = monitor["Name"].ToString();
String MonitorId = monitor["DeviceId"].ToString();
Console.WriteLine("Monitor name: {0}", MonitorName);
Console.WriteLine("Monitor id: {0}", MonitorId);
}
}
ManagementClass managementClass = new ManagementClass("Win32_BIOS");
ManagementObjectCollection instances = managementClass.GetInstances();
foreach (ManagementBaseObject instance in instances)
{
string version = instance.Properties["SMBIOSBIOSVersion"].Value.ToString();
Console.WriteLine("The version is :" + version.ToString());
}
I read a lot and browsed various topics and forums, but nowhere can I find information or is it possible at all? Maybe there is at least one serial number which will be repeated on the main machine as well as on the virtual one?
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++;
}
}
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 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
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?