Friday, January 4, 2013

Disable USB drives using C#

The C# code below is capable of enabling/disabling the USB drives of computers running in Windows XP, Windows Vista or Windows 7.
using Microsoft.Win32;

namespace USB
{
    class Program
    {
        enum Permission { Allow = 3, Deny = 4 };

        static void Main(string[] args)
        {            
            RegistryKey key;

            key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\services\USBSTOR", true);

            key.SetValue("Start", (int)Permission.Deny);
        }
    }
}
*** Using the Microsoft.Win32 namespace you will be able to retrieve and modify the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\USBSTOR registry key, Setting the value data of Start to 4 will disable the USB drives of the PC.