Exclusion of USB devices from locked USB ports in VB.NET

Apostolos Doudakmanis 121 Reputation points
2024-01-19T13:34:33.8533333+00:00

Hello With the following code I enable and disable all USB ports

Sub EnableUsbPort()
        Registry.SetValue("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR", "Start", 3, RegistryValueKind.DWord)
    End Sub

Sub DisableUsbPort() 
       Registry.SetValue("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR", "Start", 4, RegistryValueKind.DWord)

how can I exclude a device from the rest of the disabled USB ports in order for it to work e.g. a USB flash drive, or a printer

Developer technologies | VB
Developer technologies | Visual Studio | Other
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2024-01-23T07:22:33.8733333+00:00

    Hi @Apostolos Doudakmanis ,

    Try the following code to see if it helps.

        Sub AllowSpecificDevice(deviceInstanceId As String)
            ' Modify the registry to allow a specific USB device
            Dim keyPath As String = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Enum"
    
            ' Append the device instance path to the key path
            keyPath = System.IO.Path.Combine(keyPath, deviceInstanceId)
    
            ' Modify the registry to enable this specific device
            Registry.SetValue(keyPath, "Start", 3, RegistryValueKind.DWord)
        End Sub
    

    The AllowSpecificDevice subroutine requires the deviceInstanceId parameter, which you would need to obtain by examining the Device Manager or using other methods to identify the specific USB device you want to allow.

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Pinaki Ghatak 5,600 Reputation points Microsoft Employee Volunteer Moderator
    2024-01-19T13:41:35.1166667+00:00

    Hello @Apostolos Doudakmanis
    The code you’ve provided disables or enables all USB ports by modifying the Start value in the USBSTOR registry key. This is a system-wide setting and does not allow for individual control of USB ports or devices. Unfortunately, Windows does not provide a built-in method to disable or enable individual USB ports or devices through the registry. The USB ports are managed by the motherboard’s chipset and the settings are typically controlled in the BIOS, not the operating system. However, you can control access to specific USB devices using third-party software solutions that can block or allow USB devices based on various attributes like device ID, device type, etc. Please note that using such software should be done with caution and understanding of the potential risks and effects on your system. If you’re trying to secure your system, you might want to consider other methods as well, such as physical port locks or disabling USB ports in the BIOS, if supported by your hardware. I hope this answers your question.


  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.