1

I want to block a USB based on volume using C#. Like I want to block USB stick if capacity is greater than 8GB.

Look there is a method to block USB on PC using registry. but this will make USB undetectable so I can not get volume information.

I want to do that If my client program is running on some machine and I put restriction that USB capacity >=8GB should be restricted, so my C# code should safely remove the USB and should now show any balloon, I mean silently remove it.

AZ_
  • 21,688
  • 25
  • 143
  • 191
  • This question needs a little more context. Can you revise your question to explain what the program is doing and where USB blocking fits into it? – Paul Turner Oct 20 '10 at 08:28
  • Registry method will make USB undetectable, so I can not get volume information. – AZ_ Oct 20 '10 at 08:29
  • What registry method are you using? – kyndigs Oct 20 '10 at 08:34
  • It seems like you are diving into driver space here... – Daniel Mošmondor Oct 20 '10 at 08:35
  • @kyndigs Registry Method open run and paste "REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR" /v Start /t REG_DWORD /d 4 /f" and for enabling "REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR" /v Start /t REG_DWORD /d 3 /f" – AZ_ Oct 20 '10 at 08:37
  • Is there any way to hook system32\DRIVERS\USBSTOR.SYS ??? – AZ_ Oct 20 '10 at 08:37
  • I got very interesting article; have a look http://www.codeproject.com/KB/system/usbeject.aspx – AZ_ Oct 21 '10 at 04:45

1 Answers1

1

If you are disabling the USBSTOR key, then you prevent "ACCESS" to it, that includes gathering information from it.

I suggest you look here: WM_DEVICECHANGE

Using this you can catch when the USB is entered and then get the drive letter and use the DEV_BROADCAST_VOLUME to gather information on it, then you can disable it using your registry method.

kyndigs
  • 3,074
  • 1
  • 18
  • 22
  • Since the question is tagged as C#, it would be good to know if there are .NET equivalents for these Win32 events too. – Paul Turner Oct 20 '10 at 09:05
  • You can use the System.Management namespace shown here: http://msdn.microsoft.com/msdnmag/issues/02/05/WMIMan/default.aspx – kyndigs Oct 20 '10 at 09:11
  • Furthermore how to get the Event of Inserting USB so that my program get activated? – AZ_ Oct 20 '10 at 10:25
  • 1
    Look here for some examples: http://stackoverflow.com/questions/3772337/drive-is-mounted-or-changes-state-wm-devicechange-for-net – kyndigs Oct 20 '10 at 12:40
  • I have found very interesting article have a look please http://www.codeproject.com/KB/system/usbeject.aspx – AZ_ Oct 21 '10 at 04:46