I am looking solution for display custom message to the user whenever any external device (USB,Bluetooth,HDMI,etc) connected to the pc using the csharp application I tried with WMIQuery but I could not reach ,Is any solution available?
Loading
I am looking solution for display custom message to the user whenever any external device (USB,Bluetooth,HDMI,etc) connected to the pc using the csharp application I tried with WMIQuery but I could not reach ,Is any solution available?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sachin SinghRank #66 · 26.2K reputationPosted Jan 11, 2024, 10:37 AM EST
check if this helps
// Set up WMI event query to detect device connection events string query = @"SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2"; // Create a management event watcher using (ManagementEventWatcher watcher = new ManagementEventWatcher(query)) { // Register an event handler for the arrival of a new device watcher.EventArrived += (sender, e) => { // Get the device name and description string deviceName = e.NewEvent.Properties["DeviceName"].Value.ToString(); string deviceDescription = e.NewEvent.Properties["Description"].Value.ToString(); Console.WriteLine($"External device connected!\nName: {deviceName}\nDescription: {deviceDescription}"); // Add your custom logic here to display a custom message or perform any action // For example, you can use MessageBox.Show() to display a message box }; // Start listening for events watcher.Start(); Console.WriteLine("Listening for device connection events. Press Enter to exit."); Console.ReadLine(); // Stop listening when the application is closed watcher.Stop(); }Sachin SinghRank #66 · 26.2K reputationPosted Jan 11, 2024, 10:21 AM EST
Test this
// Set up WMI event query to detect device connection events string query = @"SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2"; // Create a management event watcher using (ManagementEventWatcher watcher = new ManagementEventWatcher(query)) { // Register an event handler for the arrival of a new device watcher.EventArrived += (sender, e) => { Console.WriteLine("External device connected!"); // Add your custom logic here to display a custom message or perform any action // For example, you can use MessageBox.Show() to display a message box }; // Start listening for events watcher.Start(); Console.WriteLine("Listening for device connection events. Press Enter to exit."); Console.ReadLine(); // Stop listening when the application is closed watcher.Stop(); }Janani Veeramanikandan42 reputationPosted Jan 11, 2024, 10:26 AM EST
How can get device name which was connected
Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 11, 2024, 11:37 PM EST
Try below code:
using System; using System.Management; class Program { static void Main() { // Set up a WMI event watcher to monitor USB device connection ManagementEventWatcher watcher = new ManagementEventWatcher(); WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2"); watcher.EventArrived += (sender, e) => { ManagementBaseObject targetInstance = (ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value; if (targetInstance != null) { string deviceName = targetInstance["Name"] as string; Console.WriteLine($"External device connected: {deviceName}"); // You can display your custom message or perform other actions here } }; watcher.Query = query; watcher.Start(); Console.WriteLine("Press Enter to exit."); Console.ReadLine(); watcher.Stop(); } }Thanks
Jayraj ChhayaRank #304 · 8.2K reputationPosted Jan 12, 2024, 1:44 AM EST
To display a custom message when an external device is connected to a PC using a C# application, you can utilize the
ManagementEventWatcherclass from theSystem.Managementnamespace. This class allows you to monitor WMI events and execute code when a specific event occurs.Here's an example of how you can achieve this:
using System; using System.Management; class Program { static void Main(string[] args) { // Create a WMI query to monitor device connection events string query = "SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2"; // Create a management event watcher with the query ManagementEventWatcher watcher = new ManagementEventWatcher(query); // Set the event handler for when a device connection event occurs watcher.EventArrived += DeviceConnected; // Start monitoring for device connection events watcher.Start(); // Wait for user input to exit the program Console.WriteLine("Press any key to exit..."); Console.ReadKey(); // Stop monitoring for device connection events watcher.Stop(); } static void DeviceConnected(object sender, EventArrivedEventArgs e) { // Get the device name from the event string deviceName = e.NewEvent.Properties["TargetInstance"].ToString(); // Display a custom message to the user Console.WriteLine($"Device connected: {deviceName}"); } }In this example, we create a
ManagementEventWatcherobject with a WMI query that filters for device connection events (EventType = 2). We then set an event handler (DeviceConnected) to execute code when a device connection event occurs. Inside the event handler, you can customize the code to display your desired custom message to the user.Remember to add a reference to the
System.Managementassembly in your C# project.This solution allows you to monitor and respond to external device connection events in real-time within your C# application.