Enable or Disable USBs using C#

Nevin Sunny 0 Reputation points
2025-01-29T11:22:53.4433333+00:00

Hi All,

We are trying to change the state (enable/disable) of usb devices using their PNPID. We are doing this by using the PNPUTIL. It works fine in most cases but, few devices require a reboot to change their state. This is not acceptable from a security perspective.

We do not want to block all USB devices but rather only some based on some filters.

These devices might need to be enabled if those conditions change.

We also tried the devcon utility and SetupDiCallClassInstaller calls, but they all sometimes require reboot. Is there any other API which we can use which does not require a reboot to disable or enable a USB stick?

We also tried the cfgmgr

                    CM_Enable_DevNode(DevInst, 0)

                    CM_Disable_DevNode(DevInst, cfgmgr.CM_DISABLE_PERSIST)

but this also sometimes ask for a reboot.

Any help would be appreciated.

Thank you!

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,108 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 24,801 Reputation points Microsoft Vendor
    2025-01-30T05:00:42.3066667+00:00

    @Nevin Sunny, Welcome to Microsoft Q&A, you could try to use Devcon.exe to Enable and Disable USB devices without rebooting.

    Here are some steps you could refer to.

    First, please install the WDK from the following link:

    Download the Windows Driver Kit (WDK)

    Usually is found in C:\Program Files (x86)\Windows Kits\10\Tools\x64\devcon.exe

    Second, you could find the USB Hardware device ID from Device Manager-USB Device->Properties ->Details->Hardware IDs

    Third, you could use the following c# code to call devcon.exe.

    using System;
    using System.Diagnostics;
    class Program
    {
        static void Main()
        {
            string devconPath = @"C:\Path\To\devcon.exe"; // Replace with actual path
            string hardwareId = "USB\\VID_1234&PID_5678"; // Your USB device ID
            // Disable the USB device
            ExecuteDevcon(devconPath, "disable", hardwareId);
            Console.WriteLine("USB device disabled!");
            // Wait for 3 seconds, then enable it again
            System.Threading.Thread.Sleep(3000);
            ExecuteDevcon(devconPath, "enable", hardwareId);
            Console.WriteLine("USB device enabled!");
        }
        static void ExecuteDevcon(string devconPath, string command, string hardwareId)
        {
            ProcessStartInfo psi = new ProcessStartInfo
            {
                FileName = devconPath,
                Arguments = $"{command} \"{hardwareId}\"",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                CreateNoWindow = true
            };
            using (Process process = new Process { StartInfo = psi })
            {
                process.Start();
                string output = process.StandardOutput.ReadToEnd();
                string error = process.StandardError.ReadToEnd();
                process.WaitForExit();
                Console.WriteLine(output);
                if (!string.IsNullOrEmpty(error))
                {
                    Console.WriteLine("Error: " + error);
                }
            }
        }
    }
    

    Based on my test, I could use the above code to enable or disable USB devices successfully without rebooting the system.

    Hope my advice could help you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 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.


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.