Get all connected device information - c#

I want to get all information of connected devices as the below picture using C# (device name, location, status is required).
https://i.stack.imgur.com/8cFXs.png
Please help me with this issue.

Retrieve using WMI.
You need to install System.Management from nuget.
Computer System Hardware Classes
Here is an example of getting keyboard information.
It is unclear how Location is obtained, but it is probably determined from the DeviceID string.
using System;
using System.Management;
using System.Runtime.Versioning;
internal class Program
{
[SupportedOSPlatform("windows5.0")]
private static void Main()
{
var scope = new ManagementScope("\\\\.\\ROOT\\cimv2");
var query = new ObjectQuery("SELECT * FROM Win32_Keyboard");
using (var sercher = new ManagementObjectSearcher(scope, query))
using (var queryCollection = sercher.Get())
{
foreach (var o in queryCollection)
{
Console.WriteLine(o["Description"].ToString());
Console.WriteLine(o["Status"].ToString());
Console.WriteLine(o["DeviceID"].ToString());
o.Dispose();
}
}
}
}

Related

How to get a PCIE device's link speed on Windows 7/8 programmatically

On Windows 8 when I right click on a PCIE device in Device Manager, in the Details tab, under property "PCI current link speed" I can read the PCIe link speed. The same can be done for the PCIe link width.
I'd like to access this information programmatically in a C# application. How do I do that? through WMI?
And will the same work on Windows 7?
Hope this kicks you a little:
using System;
using System.Management;
namespace PCIeSpeedExample
{
class Program
{
static void Main(string[] args)
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\cimv2", "select * from Win32_NetworkAdapter");
foreach (ManagementObject obj in searcher.Get())
{
Console.WriteLine("--------------- Adapter ----------------");
foreach (PropertyData pd in obj.Properties)
{
Console.WriteLine("{0} = {1}", pd.Name, pd.Value);
}
}
Console.Read();
}
}
}

Get name of bluetooth device on the Devices and Printers window

I know there are questions out there referring to gathering the friendly name of a device on the Device Manager but I cannot do this as the device is simply referred to as "Stardard Serial over Bluetooth link (COM)" and I have many virtual ports with the same reference. I want the name of the device as shown on the Devices and Printers window on:
I'm doing this in C# and currently just getting a list of available COM ports on the system and selecting the one I know from memory.
I managed to get it to work using 32Feet.Net.
You can search for a device by doing
BluetoothClient client = new BluetoothClient();
devices = client.DiscoverDevicesInRange();
foreach (BluetoothDeviceInfo d in devices)
{
items.Add(d.DeviceName);
}
This will give a list of the friendly names you see on the Devices and Printers window rather than "Standard serial over Bluetooth Link".
If you want the COM port like me or any other piece of information then you can simply do a WMI query such as
System.Management.ManagementObjectSearcher Searcher = new System.Management.ManagementObjectSearcher("Select * from WIN32_SerialPort");
foreach (System.Management.ManagementObject Port in Searcher.Get())
{
//your comparison or code here
}
I managed to get the bluetooth name, address and the COM port number without using the 32feet.net library by fiddling with the registry key.
Then, you could connect the bluetooth device by using the SerialPort class by passing the COM port number.
The pseudo code to obtain the bluetooth information is below:
enumerate all the COM port available in the PNP
obtain the device classGuid
search the bluetooth address from the classGuid
when the bluetooth address is known, the bluetooth name can be obtained from - this registry SYSTEM\CurrentControlSet\Services\BTHPORT\Parameters\Devices
I have posted my code in the link below:
https://stackoverflow.com/a/36298213/2297825
I'm using my custom code using 32feet.Net Library which helps me to get the Device Friendly Name also COM port information attached with that device in C# Console Application.
I'm using below code to detect Topaz-Signature Device, and its friendly Name is "T-S460-BT2". You can replace this
string FriendlyDeviceName = "T-S460-BT2";
in the code with your device name you want to search.
using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Bluetooth.Widcomm;
using InTheHand.Net.Sockets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Text.RegularExpressions;
namespace SearchDevice
{
class Program
{
static void Main(string[] args)
{
string FriendlyDeviceName = "T-S460-BT2";
if (BluetoothRadio.IsSupported)
{
BluetoothClient client = new BluetoothClient();
BluetoothDeviceInfo[] devices;
devices = client.DiscoverDevicesInRange();
foreach (BluetoothDeviceInfo d in devices)
{
if (Regex.IsMatch(d.DeviceName, FriendlyDeviceName, RegexOptions.IgnoreCase))
{
try
{
string query = string.Format("SELECT Name, DeviceID, PNPDeviceID from WIN32_SerialPort");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection osDetailsCollection = searcher.Get();
foreach (ManagementObject mo in osDetailsCollection)
{
string PNPDeviceID = (string)mo.GetPropertyValue("PNPDeviceID");
if (PNPDeviceID != null && Regex.IsMatch(PNPDeviceID, d.DeviceAddress + "", RegexOptions.IgnoreCase))
{
Console.WriteLine("{0}", ((string)mo.GetPropertyValue("DeviceId")).Replace("COM", ""));
}
}
}
catch (Exception exx)
{
}
}
}
}
else
{
Console.WriteLine("Not Supported");
}
Console.ReadLine();
}
}
}

RallyApi.Net Getting Workspaces from a Subscription Version 2.0

Following this link How to obtain a list of workspaces using Rally REST .NET
I tried the example however when I try to query against sub["Workspaces"] I get the error
RuntimeBinderException was unhandled;
The best overloaded method match for 'Rally.RestApi.RallyRestApi.Query(Rally.RestApi.Request)' has some invalid arguments
I cannot find any other ways to gather a list of workspaces from the subscription using the RallyApi dll for .Net which I obtained from the link provided.
Any help will be much appreciated.
Try to modify that code as follows:
Request wRequest = new Request(sub["Workspaces"]);
QueryResult queryResult = restApi.Query(wRequest);
Here is an entire app:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using Rally.RestApi;
using Rally.RestApi.Response;
namespace Rest_v2._0_test
{
class Program
{
static void Main(string[] args)
{
//Initialize the REST API
RallyRestApi restApi;
restApi = new RallyRestApi("user#co.com", "secret", "https://rally1.rallydev.com", "v2.0");
//get the current subscription
DynamicJsonObject sub = restApi.GetSubscription("Workspaces");
Request wRequest = new Request(sub["Workspaces"]);
//query the Workspaces collection
QueryResult queryResult = restApi.Query(wRequest);
foreach (var result in queryResult.Results)
{
var workspaceReference = result["_ref"];
var workspaceName = result["Name"];
Console.WriteLine( workspaceName + " " + workspaceReference);
}
}
}
}

How can I determine the SATA channel for a given disk?

Using DISKPART command line utility, I can get something called a "Location path" which appears to give me what I need, you can view this by using the command detail disk after selecting one of your disks in diskpart.
It appears I can get this information programatically via this class: MSFT_Disk
I am unsure about how to get an instance of this class. I have a few examples of using a ManagementObjectSearcher for WMI classes but that method is not working for me, I am also unsure of MSFT_Disk's availability in Windows 7 as the page mentions that this is for Windows 8.
Does anyone know of a good way to get SATA channel information or the "location path" of a disk?
If you want to not require Windows 8, I believe WMI is the way to go:
using System;
using System.Linq;
using System.Management;
namespace DiskScanPOC
{
class Program
{
static void Main()
{
var managementScope = new ManagementScope();
//get disk drives
var query = new ObjectQuery("select * from Win32_DiskDrive");
var searcher = new ManagementObjectSearcher(managementScope, query);
var oReturnCollection = searcher.Get();
//List all properties available, in case the below isn't what you want.
var colList = oReturnCollection.Cast<ManagementObject>().First();
foreach (var property in colList.Properties)
{
Console.WriteLine("Property: {0} = {1}", property.Name, property.Value);
}
//loop through found drives and write out info
foreach (ManagementObject oReturn in oReturnCollection)
{
Console.WriteLine("Name : " + oReturn["Name"]);
Console.WriteLine("Target Id: " + oReturn["SCSITargetId"]);
Console.WriteLine("Port: " + oReturn["SCSIPort"]);
}
Console.Read();
}
}
}
I didn't crack open my case to verify the SATA port numbers, but the above app looks like it gives reasonable results on my machine with 3 SATA hard drives.
If you want to get the location path, SetupDiGetDeviceRegistryProperty is the function you're looking for. Set the property value to SPDRP_LOCATION_INFORMATION.
I'm assuming you already know how to enumerate devices to get the DeviceInfoSet and DeviceInfoData.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;
namespace Hard_Disk_Interface
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCheck_Click(object sender, EventArgs e)
{
WqlObjectQuery q = new WqlObjectQuery("SELECT * FROM Win32_IDEController");
ManagementObjectSearcher res = new ManagementObjectSearcher(q);
lblHDDChanels.Text = string.Empty;
foreach (ManagementObject o in res.Get())
{
string Caption = o["Caption"].ToString();
lblHDDChanels.Text += Caption + "\n\n";
if (Caption.Contains("Serial"))
{
lblInterface.Text = "S-ATA";
}
}
}
}
}
Note: First Add the reference of System.Management.dll of .net freamwork 4.0

Search For Win32_Directory using the Volume Guid

I am trying to check if a directory exists on a remote computer by using the Volume GUID rather than the Volume Name. Is there any way in WMI to accomplish this? I have a tried a few different approaches but my WQL syntax never seems to be correct, most likely becuase of poor character escaping. Below is one of my attempts at the problem:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ObjectQuery query = new ObjectQuery("Select * From Win32_Directory Where Name=\"" + #"\\\\?\\Volume{10b4259b-f659-11df-b8cc-806e6f6e6963}\\test" + "\"");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection coll = searcher.Get();
foreach (ManagementObject mo in coll)
{
Console.Write("found");
}
Console.Read();
}
}
}
When I execute this code, I receieve an Invalid Query exception. I have tried numerous Powershell attempts at this query and have received the same Invalid Query exception as well. Has anyone attempted this in the past or is it even possible?
Thanks,
Chris

Categories

Resources