How to make a simple USB Desktop Locker in C#

Today I will show you how to make a USB locker for your desktop in C# Windows Application which will keep your computer unlock unless until you plugged out the pen drive. Sounds interesting?  You will find this kind of software in the Internet by search it in Google or in any other search engine. But it comes more happiness when you did it yourself. So lets come make our own USB Desktop locker.

You may also like these :

How-to-make-a-simple-USB-Desktop-Locker-in-C#

How to do?

First of all take a pen drive, don’t get frighten, your pen drive will be safe and workable. Then open the Visual Studio and add a new Windows Application project. As a form is added by default, so you don’t need to add any more. Its enough.

Before proceeding you need to clear the logic I am using to make this. Very easy one. I put a file, some written on it(as per you). And put that file into a specific location of the pen drive. When we run the program  it will search for that file content in that specific path. And of course it will search in the all removable disks plugged into your machine.

So first we need to know how to get the list of all the removable disks.  Lets check the following C# code for that.

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

I found this on Google. Now you have to check for the file and its content.

So, for that I did this one. Simply file read and before that check the file is present or not.

foreach (System.IO.DriveInfo drive in System.IO.DriveInfo.GetDrives())
{
if ((drive.DriveType == System.IO.DriveType.Removable))
{
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();
}
}
}

As I told its very easy. I thing its known to most of the reader. If its first time for you, its better for you. 🙂

Now what?

If the file contents the data written within itself then do nothing else lock the computer.

So check the file contents or not. You might have notice that I have taken a string mySting. It holds the data of the files found. Suppose in other pen drive also content the same file in the same file location it will add the content into the string myString. Now we have to check if the myString contents the actual string(your data) or not.

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

Now its clear how to check the string content, I did it here statically. You can do it dynamically by checking from another file. now one thing is left…Lock the computer. So how to do this? Again I found this in Google after a long search. Lets have a look.

using System.IO;
using System.Runtime.InteropServices;
[DllImport("user32.dll", SetLastError = true)]
static extern bool LockWorkStation();
if (myString.Contains("abc"))
{
// do nothing
}
else
{
bool result = LockWorkStation();
if (result == false)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}

Now one of the most important thing is remain. Add a timer into your web form. After adding this set Enable = true and set the time to 1000(1000ms = 1sec). And the within the timer tick method write all the code. Now run your program after after plugged pen drive. I did it in a very simple way now its up to you make a stronger one ans share here.

Download

Source: www.aspdotnet-suresh.com

Leave a Reply

Your email address will not be published. Required fields are marked *