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







ADODB CONNECTION and RECORDSET IN C#.NET


Introduction This article talks you through the usage of ADODB connection in .NET application using C# language through SQLEXPRESS. The example details the data access using ADODB, fetching recordset, insert record into database

To download the Source Code Click Here
Step  1:  add Reference  for ADODB










Step 2 USE  ADODB IN PROGRAM



*
 *  THISH IS DEMO OF ADODB CONNECTION IN C#.NET USING SQLEXPRESS
 * AND SQL SERVER
 * IN THIS PROGRAM SHOW HOW TO RETRIEVE RECORD USING ADODB RECORDSET
 * AND EXECUTE QUERY
 * AUTHER : SATYAVEER SINGH
 * EMAIL: satya.parmar@gmail.com
 * INDIA
 *
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using ADODB;  // ADODB
namespace ADODBCONNECTION
{
    public partial class Form1 : Form
    {
        ADODB.Connection conn;  // ADODB CONNECTION
        bool flag;
        string sql;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
           // string str = "Provider=SQLOLEDB.1;uid=sa;password=1;database=Student;DataSource={local}";
            string str="Provider=SQLOLEDB;Data Source=.\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=Student;Initial File Name=E:\\DotNetWebSite\\ADODBCONNECTION\\ADODBCONNECTION\\Student.mdf";
            conn = new ADODB.Connection();
            conn.Open(str, "", "", -1);  // connection Open
            CmdAdd.Enabled = true;  // Enable and Desable the buttons
            cmdDel.Enabled = true;
            cmdmodify.Enabled = true;
            cmdCan.Enabled = false;
            cmdCan.Enabled = false;
            cmdSave.Enabled = false;
            groupBox1.Enabled = false;
            fillrecord();
        }
        private void CmdAdd_Click(object sender, EventArgs e)
        {
            sql = "select max(rollno) as rn from detail";
            int num;
            string str;
            ADODB.Recordset rs = new ADODB.Recordset();
            groupBox1.Enabled = true;
            groupBox3.Enabled = false;
            CmdAdd.Enabled = false;
            cmdDel.Enabled = false;
            cmdmodify.Enabled = false;
            cmdCan.Enabled = true;
            cmdCan.Enabled = true;
            cmdSave.Enabled = true;
            txtage.Text = "";
            txtclass.Text = "";
            txtname.Text="";
            flag = false;
            rs.Open(sql, conn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockBatchOptimistic, 0);
            if (rs.Fields[0].Value == null)
            {
                num = 1;
            }
            else
            {
                num = int.Parse(rs.Fields["rn"].Value.ToString()) + 1;
            }
            if(num>=0 && num<10)
            {
                str = "00" + num.ToString();
            }
            else if (num >= 10 && num < 100)
            {
                str = "0" + num.ToString();
            }
            else
            {
                str = num.ToString();
            }
            txtroll.Text = str;
        }
        private void cmdCan_Click(object sender, EventArgs e)
        {

            CmdAdd.Enabled = true;
            cmdDel.Enabled = true;
            cmdmodify.Enabled = true;
            cmdCan.Enabled = false;
            cmdCan.Enabled = false;
            cmdSave.Enabled = false;
            if (flag == false)
            {
                txtage.Text = "";
                txtclass.Text = "";
                txtname.Text = "";
                txtroll.Text = "";
            }
            groupBox1.Enabled = false;
            groupBox3.Enabled = true;
            flag = false;
        }

        private void cmdmodify_Click(object sender, EventArgs e)
        {
            flag = true;
            groupBox3.Enabled = false;
            groupBox1.Enabled = true;
            CmdAdd.Enabled = false;
            cmdDel.Enabled = false;
            cmdmodify.Enabled = false;
            cmdCan.Enabled = true;
            cmdCan.Enabled = true;
            cmdSave.Enabled = true;
        }
        private void cmdSave_Click(object sender, EventArgs e)
        {
            string sql;
            object ret;
            if (flag == false)
            {//  Sql query for insert data
                sql = "insert into detail values('" + txtroll.Text + "','" + txtname.Text + "','"+ txtclass.Text + "'," + txtage.Text + ")";
            }
            else
            {//  Sql query for ipdate data
                sql = "update detail set name='" + txtname.Text + "',class='" + txtclass.Text + "',age=" + txtage.Text + " where rollno='" + txtroll.Text + "'";
            }
            conn.Execute(sql,out ret,0);  // Execute the query  through ADODB CONNECTION
             CmdAdd.Enabled = true;
            cmdDel.Enabled = true;
            cmdmodify.Enabled = true;
            cmdCan.Enabled = false;
            cmdCan.Enabled = false;
            cmdSave.Enabled = false;  
            groupBox1.Enabled = false;
            groupBox3.Enabled = true;
            flag = false;
            fillrecord();
        }
        private void cmdDel_Click(object sender, EventArgs e){          
            object ret;
            if (txtroll.Text == "")
            {
                MessageBox.Show("there is no record to delete plz select record", "Alert");
            }
            else
            {//  Query To delete Record
                sql = "delete  from  detail where rollno='" + txtroll.Text + "'";
                conn.Execute(sql, out ret, 0);
                txtage.Text = "";
                txtclass.Text = "";
                txtname.Text = "";
                txtroll.Text = "";
                fillrecord();
            }
        }
        private void fillrecord()   // Show tha all Record
        {
            ADODB.Recordset rs = new ADODB.Recordset();  // ADODB RECORDSET
            sql = "select rollno,name from detail";
            // RECORDSET OPEN
            rs.Open(sql,conn,ADODB.CursorTypeEnum.adOpenStatic,ADODB.LockTypeEnum.adLockBatchOptimistic,0);
            listBox1.Items.Clear();
            while (rs.EOF == false)
            {
                listBox1.Items.Add(rs.Fields[0].Value.ToString() + " ||  " + rs.Fields[1].Value.ToString());
                rs.MoveNext();
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string rollno;
            rollno = listBox1.SelectedItem.ToString().Substring(0,3);
            ADODB.Recordset rs = new ADODB.Recordset();
            sql = "select * from detail where rollno='" + rollno + "'";
            rs.Open(sql, conn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockBatchOptimistic, 0);
            txtroll.Text = rollno;
            txtname.Text = rs.Fields[1].Value.ToString();
            txtclass.Text = rs.Fields[2].Value.ToString();
            txtage.Text = rs.Fields[3].Value.ToString();
        }

    }
}
/////////
MAIN WINDOW





To download the Source Code Click Here



Love of nature...

Rain is not only drops of water.
It is the LOVE of sky 4 earth.
They never meet each other but send LOVE this way.
Enjoy the LOVE of nature... :)

Defination of love

What is LOVE ?

Love is when my mom comes to me at night & say
"Beta,
I love you"


Love is when I come back from Collage & papa says
"Beta,
late hone wala tha to call kar deta?"


Love is when my
sister says,
"Bhai,meri shadi ke baad mujse jhagda kaun karega?"


Love
is when I'm moodless & my brother says,
"Chal kahi ghumne jate
hai"


Love is when my best friends call me & say,
"Ooe !
tere bina dil nahi lagta"


That's love...
DONT MISS IT IN LIFE


LovE
iS NoT onLY Having GF or BF..
dis love is betteR ThaN HAviNg GF or BF..

Welcome Note

You are most Welcome on my blog. this is my first post for my blog

About Me

Total Pageviews

Followers