Most of our computers have sophisticated desktop locking tricks. Even in the older style Windows 98 we did have 'Password protected screensavers'. Why were they for? When we step away of computers there is a dire need to protect the data and the programs that are running in our computers from unauthorized access and prying eyes.

Windows provide a quick shortcut Windows + L to lock the workstation. So you save a few milli-seconds from using CTRL+ALT+DELETE and choosing Lock Workstation/Lock Computer (as it is called differently in different versions of Windows) Nevertheless we still feel lazy to use the two keys to lock the workstation and we get victimized by the minimum ('shoulder surfing').

I hence thought I would write a small C# code which you can have it in the Quick Launch bar. When you are moving away from the computer just click it and it will lock your workstation. The simple C# code is as below:

 

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace WindowsLock
{
    class Program
    {

        [DllImport("User32.Dll", EntryPoint = "LockWorkStation")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool LockWorkStation();

        static void Main(string[] args)
        {

            try
            {
                LockWorkStation();
                Environment.Exit(0);
            }
            catch (Exception LockWorkstationException)
            {
                Debug.WriteLine(LockWorkstationException.Message);
            }
        }
    }
}