Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help actually I made a usb locker which locks desktop whenever the specific usb is pulled out and unlocks when plugged in but it does not seem to go with usb whenever i start it; it locks pc directly and usb does not seem to work.

I used these codes
C#
foreach (System.IO.DriveInfo drive in System.IO.DriveInfo.GetDrives())
{
	if ((drive.DriveType == System.IO.DriveType.Removable))
	{
	}
}

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

bool result = LockWorkStation();

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

I also used this code to have a password stored in my drive :
C#
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("91999"))
{
    // do nothing
}
else
{
	// lock computer
}

What I want my application do do is show all the removable drives in a dropdown and then I can choose which drive to use for log in and log out.
Help Please
Posted
Updated 2-Sep-15 20:00pm
v2

1 solution

You can lock the machine quite easily. You can NOT, however, unlock it. That requires user interaction and their credentials.

Using a disk USB is not a good choice as you can use any old USB drive to fool the code.

I used a Bluetooth device to do this in the past, like 10 years ago. No, I don't have the code any more.
 
Share this answer
 
Comments
KillFlash 4-Sep-15 0:56am    
Have look again I updated my codes
Dave Kreskowiak 4-Sep-15 11:29am    
Your code will never work. You already have to be logged in in order to even pick the drive that will have the text file on it that has the password. The password itself is completely irrelevant because you cannot unlock the computer from your code!

The best you can do is lock the computer if you remove the drive from the machine. Your code will have to poll for that being the case every few seconds, handle all kinds of possible errors correctly and plugging the drive back in will do absolutely nothing at all for you.
KillFlash 4-Sep-15 13:29pm    
Thank You Very Much; BTW, I have made it work

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900