Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

How to make a simple USB Desktop Locker in C#

Oct 28, 2014
Introduction :
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.

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. :)

https://app.box.com/s/1vrkj1z3r4823kywtnb5





Arkadeep De

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

8 comments :

Anonymous said...

can you please add link to download your this project USB locker

Arkadeep De said...

just hold for moment... i am uploading the file...

Anonymous said...

verinice

Anonymous said...

<body onload="alert('hi suresh')">

Anonymous said...
This comment has been removed by a blog administrator.
john said...

I suggest to upload code to github

Rohan Nimbalkar said...

Awasome post ....

Palani M said...

Hi sir..
my requirement is when the file is copy or move and rename the file in system to removable disk. that file path, name and other details will be save in database. then report the file. please help me to develop.

we use any platform

Give your Valuable Comments

Other Related Posts

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.