Sometimes, you need to control your system from your application software. For example, you may lock the computer, log-off, reboot, shutdown, etc.
Here I am going to discuss how these things can be done in .NET.
Note: You have to use un-managed code to perform this application.
Using the Code
You have to first add the
System.Runtime.InteropServices using directive into your project.
In order to lock the system, you need to call the
LockWorkStation() method which is in user32.dll.
First, you need to import user32.dll before calling.
Here is the code for importing user32.dll. Define the function as shown below:
public static extern void LockWorkStation();
Stand By and Hibernation
SetSuspendState method of
SetSuspendState method accepts 3 arguments.
The first argument power sets either standby or hibernate, the second argument if set to
true, forces the OS to suspend all its applications. The third parameter, if set to
true, disables wake events.
Below is the code for the application to stand by:
Application.SetSuspendState(PowerState.Suspend true, true);
For the application to Hibernate, use the following code:
Application.SetSuspendState(PowerState.Hibernate, true, true);
Log-off the Application
In order to log off, you need to first import user32.dll and define the function which you need to call to log off.
public static extern int ExitWindowsEx(int uFlags, int dwReason);
ExitWindowsEx function with 2 arguments as
0, Use the following line to log off:
Rebooting the System
We use the same function
ExitWindowsEx but now with different parameters:
0 as arguments for shutting down the system:
Points of Interest
I felt happy writing this code to control my system from my .NET application.
- 25th June, 2007: Initial post
Around 10 years of professional software development experience in analysis, design, development, testing and implementation of enterprise web applications for healthcare domain with good exposure to object-oriented design, software architectures, design patterns, test-driven development and agile practices.
Analyse and create High Level , Detailed Design documents.
Use UML Modelling and create Use Cases , Class Diagram , Component Model , Deployment Diagram, Sequence Diagram in HLD.
Area of Working : Dedicated to Microsoft .NET Technologies
Experience with : C# , J2EE , J2ME, Windows Phone 8, Windows Store App
Database: Microsoft SQL Server, FoxPro
Development Frameworks: Microsoft .NET 1.1, 2.0, 3.5, 4.5
UI: Windows Forms, Windows Presentation Foundation, ASP.NET Web Forms and ASP.NET MVC3, MVC4
Coding: WinForm , Web Development, Windows Phone, WinRT Programming, WCF, WebAPI
Healthcare Domain Experience
CCD, CCR, QRDA, HIE, HL7 V3, Healthcare Interoperability
B.E (Computer Science)
CodeProject Contest So Far:
1. Windows Azure Developer Contest - HealthReunion - A Windows Azure based healthcare product , link - http://www.codeproject.com/Articles/582535/HealthReunion-A-Windows-Azure-based-healthcare-pro
2. DnB Developer Contest - DNB Business Lookup and Analytics , link - http://www.codeproject.com/Articles/618344/DNB-Business-Lookup-and-Analytics
3. Intel Ultrabook Contest - Journey from development, code signing to publishing my App to Intel AppUp , link - http://www.codeproject.com/Articles/517482/Journey-from-development-code-signing-to-publishin
4. Intel App Innovation Contest 2013 - eHealthCare
5. Grand Prize Winner of CodeProject HTML5 &CSS3 Article Contest 2014
6. Grand Prize Winner of CodeProject Android Article Contest 2014
7. Grand Prize Winner of IOT on Azure Contest 2015