Showing posts with label File Reading and Writing. Show all posts
Showing posts with label File Reading and Writing. Show all posts

Disable Pen Drives Using C# .NET


Introduction
Enable and disable the pen drives and external mass storage Device
Using C#  (CSharp) .net
And this program tell about file reading and writing 

Source code given blow
Default Password is : satya

TO Download Source Code Click Here

// for main form form1.cs

// Auther Satyveer Singh
/////  India
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


 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;
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();
           
        }
    }
}




To Download Source Code Click Here







About Me

Total Pageviews

Followers