Click here to Skip to main content
15,888,579 members
Articles / Programming Languages / C#
Tip/Trick

A Simple USB desktop locker

Rate me:
Please Sign up or sign in to vote.
4.71/5 (12 votes)
26 Oct 2014CPOL2 min read 27.2K   2.9K   23   5

Introduction

USB desktop locker is a common thing in the world of computer. Search it in Google and you will find lots of software either free or paid. In search of this kind of software  I found one very useful. But being a software developer I thought why don't I  create my own USB locker? As i thought i made one for my personal use and here it is with full source code for all of you.

How it works?

One USB drive will be plugged into your computer and whenever any one trying to plug out the pen drive the computer will be locked. After unlocking the computer if the pen drive is not plugged in then also the computer will be locked, Computer will be unlocked only if the pen drive is plugged in.

Logic is simple. I put a file into a specific location of the pen drive and an EXE is locking for that particular file's content. If it matches do nothing else lock the computer.

How to do?

So, how to do this? Very simple steps are waiting for you.

Step 1:

Add a windows project and add arrange a pen drive, probably an unused one. Add a new Win Form into your project and then add a Timer into the Win Form. Set the Enable property to true. Set the Interval to 1000(1000 ms = 1 sec).  And now generate the Timer_Tick method.

Step 2:

Now get the code to discover the all removal disk, which are plugged in the computer.

foreach (System.IO.DriveInfo drive in System.IO.DriveInfo.GetDrives())
{
	if ((drive.DriveType == System.IO.DriveType.Removable))
	{
		// all here are removable disks
	}
}

Step 3:

Now check the specified file name within the specified folder location of the all removal disks found during the search. If the file is present in the location then match the file content. Here I am checking if in the file "abc" is written or not. If found then do nothing and for else lock the part.

foreach (System.IO.DriveInfo drive in System.IO.DriveInfo.GetDrives())
{
	if ((drive.DriveType == System.IO.DriveType.Removable))
	{
		label1.Text = label1.Text + " " + drive.Name;
		string resumeFile = drive.Name + @"locker\locker.txt";

		if (File.Exists(resumeFile))
		{
			System.IO.StreamReader myFile = new System.IO.StreamReader(resumeFile);
			myString = myString + " "+myFile.ReadToEnd();
			myFile.Close();

		}
	}
}

if (myString.Contains("abc"))
{
    // do nothing
}
else
{
	// lock computer
}

Step 4:

Now if the file content mismatched or the file is missing then we have to lock the computer. How to lock that? A very simple process I found after Goggling.

[DllImport("user32.dll", SetLastError = true)]
static extern bool LockWorkStation();

bool result = LockWorkStation();

if (result == false)
{
	throw new Win32Exception(Marshal.GetLastWin32Error());
}

Step 5:

You can add a notifyIcon into your project as I added into the project. Check the following coding for this.

private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
	this.Show();
	this.WindowState = FormWindowState.Normal;
}

private void Form1_Resize(object sender, EventArgs e)
{
	notifyIcon1.BalloonTipTitle = "USB Locker";
	notifyIcon1.BalloonTipText = "Running in system tray.";

	if (FormWindowState.Minimized == this.WindowState)
	{
		notifyIcon1.Visible = true;
		notifyIcon1.ShowBalloonTip(500);
		this.Hide();
	}
	else if (FormWindowState.Normal == this.WindowState)
	{
		notifyIcon1.Visible = false;
	}
}

 

Its just a simple one. I didn't proceed to make it a strong one. Though for the layman its enough to protect your PC. If you want any type of changes, please post your valuable comments.

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer PwC
India India
I am a Software developer having an experience of 5 years in application development. To get me you can mail me at arkadeepde@gmail.com or you can visit my blog at ASP With Arka

Comments and Discussions

 
Questionusb pc locker Pin
Member 1469301917-Dec-19 19:56
Member 1469301917-Dec-19 19:56 
QuestionNice Article - One question Pin
David Mello27-Oct-14 6:47
David Mello27-Oct-14 6:47 
AnswerRe: Nice Article - One question Pin
Arkadeep De27-Oct-14 8:30
professionalArkadeep De27-Oct-14 8:30 
GeneralMy vote of 4 Pin
majid torfi26-Oct-14 5:06
professionalmajid torfi26-Oct-14 5:06 
GeneralRe: My vote of 4 Pin
Arkadeep De26-Oct-14 10:26
professionalArkadeep De26-Oct-14 10:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.