A Simple USB desktop locker

th

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.

ID.USB.Lock.Key.1.3

License

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s