Enable and disable the pen drives and external mass storage Device

This program tell about file reading and writing in system.

This module use for password read and writes from file to control password.

Source code given blow:

Default Password is : satya // for main form form1.cs

pendrive.gif

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;// Add refernce
using Microsoft.Win32;  //  for registry
using System.IO;  // for input output
namespace PenDrive
{
    public partial class Form1 : Form
    {     
        string strPath;

        public static string passwordset = "";
        public Form1()
        {
            InitializeComponent();
        }       
        private void Form1_Load(object sender, EventArgs e)
        {
            strPath = "SYSTEM\\CurrentControlSet\\Services\\usbstor"// Registry Path
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            // Set the WindowState to normal if the form is minimized.           
            this.WindowState = FormWindowState.Minimized;
            // Activate the form.
            this.Activate();
            this.ShowInTaskbar = false;
        } 
 
        private void Cancel(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            this.Activate();
            this.ShowInTaskbar=false;         
        }
        private void CmdSave_Click(object sender, EventArgs e)
        {
            RegistryKey usbkey = Registry.LocalMachine.CreateSubKey(strPath); // Create registry
            string path = Application.StartupPath.ToString();
            string fileP = path + "\\passw.txt"// for Password access
            string oldpass;

            StreamReader rw = new StreamReader(fileP);
            oldpass = rw.ReadLine();
            rw.Close();
            if (RDEnab.Checked == true)
            {
                FrmEnable ab = new FrmEnable();
                ab.ShowDialog();
                if (passwordset == "")
                {
                    return;
                }
                if (oldpass == passwordset)  // Password maatch
                {
                    usbkey.SetValue("Start", 3, RegistryValueKind.DWord);  // Set Enable Pendrivess
                    MessageBox.Show("All pen Drive ports are Enable","Success",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Wrong Password","Password",MessageBoxButtons.OK,MessageBoxIcon.Stop);
                }
            }
            else
            {
                usbkey.SetValue("Start", 4, RegistryValueKind.DWord); // Set Disable Pendrivess
                MessageBox.Show("All pen Drive ports are Disable", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            passwordset = "";
        }
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            bool clse = true;
            this.Dispose(clse);
        }
        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            // Set the WindowState to normal if the form is minimized.
            if (this.WindowState == FormWindowState.Minimized)

                this.WindowState = FormWindowState.Normal;
           this.Activate();
        }      
 
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            // Set the WindowState to normal if the form is minimized.
            this.WindowState = FormWindowState.Normal;
            // Activate the form.
            this.Activate();
            this.ShowInTaskbar = true;           
        }       
        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            this.ShowInTaskbar = false;
        }
        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
           FrmPass fr = new FrmPass();
           fr.Show();         
        }  
    }
}

Change password form source pass.cs

pendrive1.gif

using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;  //  for registry
using System.IO;  // for input output
namespace PenDrive
{
    public partial class Form1 : Form
    {       
        string strPath;
        public static string passwordset = "";
        public Form1()
        {
            InitializeComponent();
        }       
        private void Form1_Load(object sender, EventArgs e)
        {
            strPath = "SYSTEM\\CurrentControlSet\\Services\\usbstor"// Registry Path
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;

            // Set the WindowState to normal if the form is minimized.           
                this.WindowState = FormWindowState.Minimized;
            // Activate the form.
            this.Activate();
            this.ShowInTaskbar = false;
        } 
        private void Cancel(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            this.Activate();
            this.ShowInTaskbar=false;           
        }
        private void CmdSave_Click(object sender, EventArgs e)
        {
            RegistryKey usbkey = Registry.LocalMachine.CreateSubKey(strPath); // Create registry
            string path = Application.StartupPath.ToString();
            string fileP = path + "\\passw.txt"// for Password access
            string oldpass;
            StreamReader rw = new StreamReader(fileP);
            oldpass = rw.ReadLine();
            rw.Close();
            if (RDEnab.Checked == true)
            {
                FrmEnable ab = new FrmEnable();
               ab.ShowDialog();
               if (passwordset == "")
               {
                   return;
               }
                if (oldpass == passwordset)  // Password maatch
                {
                    usbkey.SetValue("Start", 3, RegistryValueKind.DWord);  // Set Enable Pendrivess
                    MessageBox.Show("All pen Drive ports are Enable","Success",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Wrong Password","Password",MessageBoxButtons.OK,MessageBoxIcon.Stop);
                }
            }
            else
            {
                usbkey.SetValue("Start", 4, RegistryValueKind.DWord); // Set Disable Pendrivess
                MessageBox.Show("All pen Drive ports are Disable", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            passwordset = "";
        }
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            bool clse = true;
            this.Dispose(clse);
        }
        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            // Set the WindowState to normal if the form is minimized.
            if (this.WindowState == FormWindowState.Minimized)
                this.WindowState = FormWindowState.Normal;
            // Activate the form.
            this.Activate();           
        }      
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            // Set the WindowState to normal if the form is minimized.
            this.WindowState = FormWindowState.Normal;
            // Activate the form.
            this.Activate();
            this.ShowInTaskbar = true;           
        }       
        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            this.ShowInTaskbar = false;
        }
        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
           FrmPass fr = new FrmPass();
           fr.Show();           
        }
    }
}

Next Recommended Reading Disable Button after Click Event Fire