saba cmp

saba cmp

  • NA
  • 10
  • 4.2k

How to block a usb device to connect to windows using c#.net

Feb 4 2017 4:05 AM
I want to create a windows application in c#.net to disable usb port by below code

Microsoft.Win32.Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR","Start",4,Microsoft.Win32.RegistryValueKind.DWord);
MessageBox.Show("Disabled");
 
after that I want to create a listbox with items of usb devices connected to windows.also for this I use belowe code
 
 
List<USBDeviceInfo> GetUSBDevices()
{
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
collection = searcher.Get();
foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description")
));
}
collection.Dispose();
return devices;
}
var usbDevices = GetUSBDevices();
foreach (var usbDevice in usbDevices)
{
listBox1.Items.Add(string.Format("Device ID: {0} Description: {1}",usbDevice.DeviceID, usbDevice.Description));//, usbDevice.PnpDeviceID, usbDevice.Description));
}
 now I want to select any item in listbox to give permission to connect to windows by its Id
I want to do this for security not to allow any usb device to connect to copy any information from windows.
So how can I enable usb device by it's Id in c#.net