How do I lock a windows workstation programmatically? [duplicate]

I haven’t tried it myself, but I found this on google

Process.Start(@"C:\WINDOWS\system32\rundll32.exe", "user32.dll,LockWorkStation");

edit: I tried it, and it works!

edit2: Here’s a solution using user32.dll that doesn’t start an external process.

using System.Runtime.InteropServices;

declare a method like this:

[DllImport("user32.dll")]
public static extern bool LockWorkStation();

and then call LockWorkStation();.

VoilĂ 

Leave a Comment