locked
Detecting if a specific USB is connected RRS feed

  • Question

  • Looking for some advice on how I can achieve the following:

    I'm currently building an C# wpf application and i want to add where the application runs and check if a specific usb is plugged in to any of the usb ports and if not the application cant be interacted till said usb is plugged in and continues to check if the usb is connected and if its remove the application locks any interaction to it till the usb is plugged in again  ?

    any ideas on how this can be done?

    class DriveDetection
        {
            // Used For Monitoring Of USB Connection 
            private ManagementEventWatcher Connected;
            private ManagementEventWatcher Removed;
            public void USBControl()
            {
                // USB Connection Event Watching
                Connected = new ManagementEventWatcher();
                // USB Removal Event Watching
                Removed = new ManagementEventWatcher();
            }
        }
    }

    this is the idea of so far but i dont know where to go from here

    • Edited by Evadingea Saturday, October 3, 2015 12:53 PM
    Saturday, October 3, 2015 12:51 PM

Answers

  • Hi Evadingea,

    You could register the event. On event method, enable or disable all the wpf windows. Code below is for your reference.

    class DriveDetection
    {
        // Used For Monitoring Of USB Connection 
        private ManagementEventWatcher Connected;
        private ManagementEventWatcher Removed;
        public void USBControl()
        {
            // USB Connection Event Watching
            Connected = new ManagementEventWatcher();
            Connected += OnConnected();
            // USB Removal Event Watching
            Removed = new ManagementEventWatcher();
            Removed += OnRemoved;
        }
    
        private void OnConnected()
        {
            //enable all the windows
            for (int i = 0; i < Application.Current.Windows.Count; i++)
            {
                Application.Current.Windows[i].IsEnabled = true;
            }
        }
    
        private void OnRemoved()
        {
            //disable all the windows
            for (int i = 0; i < Application.Current.Windows.Count; i++)
            {
                Application.Current.Windows[i].IsEnabled = false;
            }
        }
    }
    Best Regards,
    Li Wang
    • Proposed as answer by DotNet Wang Monday, October 12, 2015 9:32 AM
    • Marked as answer by Kristin Xie Thursday, October 15, 2015 9:39 AM
    Monday, October 5, 2015 7:24 AM

All replies

  • Hello Evadingea,

    You could look into the following articles on detecting USB drives with C#. Hope this helps you.

    Detecting USB Drive Removal in a C# Program

    A USB Library to Detect USB Devices

    How to detect a USB drive has been plugged in?

    Thanks,

    Sabah Shariq


    Saturday, October 3, 2015 5:41 PM
  • ive seen them but none of them come close to what im after 
    Sunday, October 4, 2015 3:56 AM
  • Hi Evadingea,

    You could register the event. On event method, enable or disable all the wpf windows. Code below is for your reference.

    class DriveDetection
    {
        // Used For Monitoring Of USB Connection 
        private ManagementEventWatcher Connected;
        private ManagementEventWatcher Removed;
        public void USBControl()
        {
            // USB Connection Event Watching
            Connected = new ManagementEventWatcher();
            Connected += OnConnected();
            // USB Removal Event Watching
            Removed = new ManagementEventWatcher();
            Removed += OnRemoved;
        }
    
        private void OnConnected()
        {
            //enable all the windows
            for (int i = 0; i < Application.Current.Windows.Count; i++)
            {
                Application.Current.Windows[i].IsEnabled = true;
            }
        }
    
        private void OnRemoved()
        {
            //disable all the windows
            for (int i = 0; i < Application.Current.Windows.Count; i++)
            {
                Application.Current.Windows[i].IsEnabled = false;
            }
        }
    }
    Best Regards,
    Li Wang
    • Proposed as answer by DotNet Wang Monday, October 12, 2015 9:32 AM
    • Marked as answer by Kristin Xie Thursday, October 15, 2015 9:39 AM
    Monday, October 5, 2015 7:24 AM
  • This is an updated C# USB/Hid library that will monitor for connections, disconnections on Android, UWP, and Windows. Here is some example code:

    Device Listener

        internal class TrezorExample : IDisposable
        {
            #region Fields
            //Define the types of devices to search for. This particular device can be connected to via USB, or Hid
            private readonly List<FilterDeviceDefinition> _DeviceDefinitions = new List<FilterDeviceDefinition>
            {
                new FilterDeviceDefinition{ DeviceType= DeviceType.Hid, VendorId= 0x534C, ProductId=0x0001, Label="Trezor One Firmware 1.6.x", UsagePage=65280 },
                new FilterDeviceDefinition{ DeviceType= DeviceType.Usb, VendorId= 0x534C, ProductId=0x0001, Label="Trezor One Firmware 1.6.x (Android Only)" },
                new FilterDeviceDefinition{ DeviceType= DeviceType.Usb, VendorId= 0x1209, ProductId=0x53C1, Label="Trezor One Firmware 1.7.x" },
                new FilterDeviceDefinition{ DeviceType= DeviceType.Usb, VendorId= 0x1209, ProductId=0x53C0, Label="Model T" }
            };
            #endregion
    
            #region Events
            public event EventHandler TrezorInitialized;
            public event EventHandler TrezorDisconnected;
            #endregion
    
            #region Public Properties
            public IDevice TrezorDevice { get; private set; }
            public DeviceListener DeviceListener { get; private set; }
            #endregion
    
            #region Event Handlers
            private void DevicePoller_DeviceInitialized(object sender, DeviceEventArgs e)
            {
                TrezorDevice = e.Device;
                TrezorInitialized?.Invoke(this, new EventArgs());
            }
    
            private void DevicePoller_DeviceDisconnected(object sender, DeviceEventArgs e)
            {
                TrezorDevice = null;
                TrezorDisconnected?.Invoke(this, new EventArgs());
            }
            #endregion
    
            #region Public Methods
            public void StartListening()
            {
                TrezorDevice?.Dispose();
                DeviceListener = new DeviceListener(_DeviceDefinitions, 3000);
                DeviceListener.DeviceDisconnected += DevicePoller_DeviceDisconnected;
                DeviceListener.DeviceInitialized += DevicePoller_DeviceInitialized;
            }
    
            public async Task InitializeTrezorAsync()
            {
                //Get the first available device and connect to it
                var devices = await DeviceManager.Current.GetDevices(_DeviceDefinitions);
                TrezorDevice = devices.FirstOrDefault();
                await TrezorDevice.InitializeAsync();
            }
    
            public async Task<byte[]> WriteAndReadFromDeviceAsync()
            {
                //Create a buffer with 3 bytes (initialize)
                var writeBuffer = new byte[64];
                writeBuffer[0] = 0x3f;
                writeBuffer[1] = 0x23;
                writeBuffer[2] = 0x23;
    
                //Write the data to the device
                return await TrezorDevice.WriteAndReadAsync(writeBuffer);
            }
    
            public void Dispose()
            {
                TrezorDevice?.Dispose();
            }
            #endregion
        }

    Friday, January 18, 2019 8:40 AM