How to disable usb port in C#
-
Monday, September 05, 2011 3:39 AM
i want to disable USB port using C#.
thanks.
Larrix
All Replies
-
Monday, September 05, 2011 4:35 AM
If you want to disable USB storage devices (flash memory, etc), you can write a program to change the following registry value:
From key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR
Change value of Start from 3 to 4.
If you have any problem writing such program, please let me know.
Visit my technical blog: http://csharptips.wordpress.com -
Monday, September 05, 2011 5:00 AM
hello Abdollah,
how to change the value Programmatically?
thank you very much.
Larrix -
Monday, September 05, 2011 5:09 AM
//disable USB storage... Microsoft.Win32.Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR", "Start", 4, Microsoft.Win32.RegistryValueKind.DWord); //enable USB storage... Microsoft.Win32.Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR", "Start", 3, Microsoft.Win32.RegistryValueKind.DWord);
Visit my technical blog: http://csharptips.wordpress.com- Marked As Answer by Larrix Monday, September 05, 2011 5:36 AM
-
Monday, September 05, 2011 5:22 AM
Hello Abdollah,
i already try the code that you have given to me.
but when i try to run it, this error appeared.
Access to the registry key 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR' is denied.
thanks.
Larrix -
Monday, September 05, 2011 5:31 AM
You must have administrator privileges to be able to change registry values.
Visit my technical blog: http://csharptips.wordpress.com -
Monday, September 05, 2011 5:36 AM
Hello Abdollah,
My account is adminstrator.
I tried on windows XP OS.. it's fine.
but for windows 7 OS not.
Anyway thank you very much for your help.
I will find ways on it.
Larrix -
Monday, September 05, 2011 5:44 AM
I suppose it's related to User Account Control.
Personally, I don't suggest it... but try setting your user account control setting to the lowest possible security.
Visit my technical blog: http://csharptips.wordpress.com -
Monday, September 05, 2011 11:45 AMRight-click your application, and select "Run as Administrator" from the context menu.
--
Mike -
Monday, September 05, 2011 1:01 PM
This is a sophisticated wish.
First things first: How do you identify the USB port? I assume there are several.
Once I've downloaded a tool call "devcon.exe" (=device console). Among others, it also offers an option to disable a device. After remembering the tool, I found that it's source code is part of the Windows Driver Kit. So, you can have a look there to find out how it works. The WDK documentation says: "The cmdEnable, cmdDisable, and cmdRestart commands show how to issue DIF_PROPERTYCHANGE to enable, disable or restart the device." Link: DIF_PROPERTYCHANGE
Some month ago, I happend to write a managed wrapper for the "device installation" section of the WDK. Well,,,, I don't remember in which condition the wrapper is currently. ;-) And due to references to some of my other libraries I won't be able to offer it to you, but I'll have a look at the source code in order to get into that matter again and find out which way you can go. Will take some time....
Edit: There are three types of device interface classes related to USB: USBDevice, USBHostController and USBHub. Which one are you referring to? In the end you will have to call SetupDiSetDeviceInterfaceProperty passing DEVPKEY_DeviceInterface_Enabled as the property key and the value FALSE as the property value to disable the device. But before reaching that goal, you will have to study the basics about device installation.
Armin
- Edited by Armin Zingler Monday, September 05, 2011 1:19 PM
- Edited by Armin Zingler Monday, September 05, 2011 1:20 PM
-
Thursday, March 29, 2012 5:16 AM
Hi Larrix,
Even i have developed the application to diable the USB, which runs successfully on XP. After migrating it on Windows 7 it fails to disable the USB. I guess it is the problem with RegistryKey.
Just want to ask you whether you got success or not??.. If yes, request you to share the code for the same.
Thanks and Regards,
Chetan Rathod
-
Thursday, March 29, 2012 5:25 AM
Hi Abdollah,
Could you please share the C# code to disable the USB on windows 7.
Thanks and Regards,
Chetan Rathod.
-
Thursday, March 29, 2012 5:26 AM
Hi Mike,
Could you please share the C# code to disable the USB on windows 7.
Thanks and Regards,
Chetan Rathod.
-
Friday, April 13, 2012 5:33 AM
Refer the below link. it might helpful
http://sourceforge.net/projects/libusbdotnet/
Manish Maheshwari (MCP)
- Proposed As Answer by ManishMaheshwari Friday, April 13, 2012 5:34 AM
-
Saturday, June 09, 2012 8:00 PM
i've tried this code on windows 7 but its not working ......
Access to the registry key 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR' is denied.
-
Friday, August 10, 2012 11:16 AM
windows 7 doesn't allow any application to be run in administrator mode by default.
The setting can be modified in User Access Control. Modifying the UAC may not be a recommended solution.
Try running your exe as an administrator. You can do this by right clicking on the exe and selecting "Run As Adminstrator" ( If you have built your application ).
If you are in development stage and want to run your program without the error run Visual Studio as an administrator before opening your project (right click on Visual studio 2008 / 2010 icon and select "Run As Adminstrator").
Hope this helps.
- Edited by samsylvester Friday, August 10, 2012 11:16 AM
- Proposed As Answer by Anurag20033 Thursday, February 07, 2013 5:58 AM
- Unproposed As Answer by Anurag20033 Thursday, February 07, 2013 5:58 AM
-
Thursday, February 07, 2013 5:57 AM
Dear Abdollah
Your code is working with windows 32 bit but not working for windows 64 bit . do you have any solution for 64 bit ....
thanks in advance
-
Thursday, May 16, 2013 11:33 AMgreat post. :)


