Usb Enable / Disable

usbEnableDisable

During the Windows 7 migration at the place I work at the desktop group decided to disable the USB ports so the users couldn’t plug any thumb drives, external HD’s, etc… They were doing this manually during the imaging process. Well a couple people actually needed this functionality and the desktop guys were spending way too much time re-enabling the ports. So I came up with this little program for them. This basically copied the files and adds the registry keys. Then when the user is done doing whatever they need to the Usb ports can be turned off easily. Future versions of this program will include the ability to connect to the computer locally. Again it’s a simple 2 class program. I also added the app.manifest on this one because of the need to be an admin on the box. However our local domain policy turns this off on the desktops. So the desktop admins still have to login to the local machine with their admin credentials. But the functionality is still there in the even the program needs to be used outside of the organization.

UsbEnableDisable Class:

//Application created by Daniel on 6.25.2013.
//Used for enabling and disabling the USB ports so storage devices such as external hard drives,
//thumbdrives, etc.., can be turned on and off.

//Note: This application does have an admin manifest file. However if the policy is not to use run-as
//you will need to be logged in as an admin to the local machine.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UsbEnableDisable
{
    public partial class UsbEnableDisable : Form
    {
        private Usb _usbEnableDisable;

        public UsbEnableDisable()
        {
            InitializeComponent();
            _usbEnableDisable = new Usb();
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void copyFilesButton_Click(object sender, EventArgs e)
        {
            //Using the Path class to manipulate the file and directory path.
            string sourceInfFile = System.IO.Path.Combine(_usbEnableDisable.SourcePath, _usbEnableDisable.InfFileName);    //The source for the INF file(s).
            string destInfFile = System.IO.Path.Combine(_usbEnableDisable.DestPath, _usbEnableDisable.InfFileName);    //The destination for the INF file(s).
            string sourcePnfFile = System.IO.Path.Combine(_usbEnableDisable.SourcePath, _usbEnableDisable.PnfFileName);    //The source for the PNF file(s).
            string destPnfFile = System.IO.Path.Combine(_usbEnableDisable.DestPath, _usbEnableDisable.PnfFileName);    //The destination for the PBF file(s).

            try
            {
                //Copying the files to their destination folders. Overwriting the files if they exist.
                System.IO.File.Copy(sourceInfFile, destInfFile, true);
                System.IO.File.Copy(sourcePnfFile, destPnfFile, true);

                //Status box messages.
                statusListBox.Items.Add(_usbEnableDisable.InfFileName + " copied from " + _usbEnableDisable.SourcePath + " to " + _usbEnableDisable.DestPath);
                statusListBox.Items.Add(_usbEnableDisable.PnfFileName + " copied from " + _usbEnableDisable.SourcePath + " to " + _usbEnableDisable.DestPath);
            }
            catch (UnauthorizedAccessException)
            {
                //Error message if the user doesn't have rights.
                MessageBox.Show("You need to logged in as an administrator.", "USB Enable / Disable");
            }
        }

        private void addRegButton_Click(object sender, EventArgs e)
        {
            try
            {
                //Creating the USB key registry subfolder.
                Microsoft.Win32.RegistryKey key;
                key = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(@"SYSTEM\CurrentControlSet\services\USBSTOR");

                //Creating the string values.
                key.SetValue("DisplayName", "USB Mass Storage Driver");
                key.SetValue("DriverPackageId", "v_mscdsc.inf_amd64_neutral_8b1e6b55729c3283");

                //Creating a REG_EXPAND_SZ key.
                key.SetValue("ImagePath", @"\SystemRoot\system32\drivers\USBSTOR.SYS", Microsoft.Win32.RegistryValueKind.ExpandString);

                //Creating the DWORD or binary values.
                key.SetValue("BootFlags", 4);
                key.SetValue("ErrorControl", 1);
                key.SetValue("Start", 3);
                key.SetValue("Type", 1);
                key.Close();

                //Status box message update.
                statusListBox.Items.Add("USB Storage Registry keys added.");
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("You need to logged in as an administrator.", "USB Enable / Disable");
            }
        }

        private void removeFilesButton_Click(object sender, EventArgs e)
        {
            try
            {
                //Deleting the file(s).
                System.IO.File.Delete(_usbEnableDisable.InfDestPath);
                System.IO.File.Delete(_usbEnableDisable.PnfDestPath);

                //Status box messages.
                statusListBox.Items.Add(_usbEnableDisable.InfFileName + " deleted from " + _usbEnableDisable.DestPath);
                statusListBox.Items.Add(_usbEnableDisable.PnfFileName + " deleted from " + _usbEnableDisable.DestPath);
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("You need to be logged in as an administrator.", "USB Enable / Disable");
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("You need to be logged in as an administrator.", "USB Enable / Disable");
            }
        }

        private void removeRegButton_Click(object sender, EventArgs e)
        {
            try
            {
                //Removing the registry key entries.
                Microsoft.Win32.Registry.LocalMachine.DeleteSubKeyTree(@"SYSTEM\CurrentControlSet\services\USBSTOR");

                //Status box message update.
                statusListBox.Items.Add("USB Storage Registry keys removed.");
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("You need to be logged in as an administrator.", "USB Enable / Disable");
            }
            catch (System.ArgumentException)
            {
                MessageBox.Show("You need to be logged in as an administrator.", "USB Enable / Disable");
            }

        }
    }
}


Usb Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UsbEnableDisable
{
    class Usb
    {
        private string infFileName = "usbstor.inf";
        private string pnfFileName = "usbstor.PNF";
        private string sourcePath = @"C:\ITPrograms\Files";    //Path for the program to get files from.
        private string destPath = @"C:\Windows\inf\";    //Path to copy the inf and pnf files. 
        private string infDestPath = @"C:\Windows\inf\usbstor.inf";    //Full path of the inf file(s).
        private string pnfDestPath = @"C:\Windows\inf\usbstor.PNF";    //Full path of the PNF file(s).

        public string InfFileName
        {
            get { return infFileName; }
            set { infFileName = value; }
        }

        public string PnfFileName
        {
            get { return pnfFileName; }
            set { pnfFileName = value; }
        }

        public string SourcePath
        {
            get { return sourcePath; }
            set { sourcePath = value; }
        }

        public string DestPath
        {
            get { return destPath; }
            set { destPath = value; }
        }

        public string InfDestPath
        {
            get { return infDestPath; }
            set { infDestPath = value; }
        }

        public string PnfDestPath
        {
            get { return pnfDestPath; }
            set { pnfDestPath = value; }
        }
    }
}

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s