In this article, I am going to show:
How to Shut Down a machine
How to Log Off a machine
How to forced log off a machine
How to restart a machine using c# To perform our task, very first let us create a windows application project. On form drag and drop four buttons for four different operations Navigate to code behind of form and add reference of System.Runtime.InteropServicesAdd a static extern method to Form.cs[DllImport("user32.dll")]
public static extern int ExitWindowsEx(int operationFlag, int operationReason); Log off the System On click event of Log Off button, call ExitWindowsEX method with proper flag. For log off operation flag value is 0. private void btnLogOff_Click(object sender, EventArgs e)
{
ExitWindowsEx(0, 0);
}Forced Log off the System On click event of Forced Log Off button, call ExitWindowsEX method with proper flag. For Forced log off operation flag value is 4. private void btnForcedLogOff_Click(object sender, EventArgs e)
{
ExitWindowsEx(4, 0);
}Shut Down the SystemOn click event of Shut down button, call ExitWindowsEX method with proper flag. For shut down operation flag value is 1. private void btnShutDown_Click(object sender, EventArgs e)
{
ExitWindowsEx(1, 0);
}Restart the System On click event of Restart button, call ExitWindowsEX method with proper flag. For Restart operation flag value is 2. private void btnRestart_Click(object sender, EventArgs e)
{
ExitWindowsEx(2, 0);
}Now when you run the application all system operation should be performed. For your reference full source code is given here
Read more: C# Corner
How to Shut Down a machine
How to Log Off a machine
How to forced log off a machine
How to restart a machine using c# To perform our task, very first let us create a windows application project. On form drag and drop four buttons for four different operations Navigate to code behind of form and add reference of System.Runtime.InteropServicesAdd a static extern method to Form.cs[DllImport("user32.dll")]
public static extern int ExitWindowsEx(int operationFlag, int operationReason); Log off the System On click event of Log Off button, call ExitWindowsEX method with proper flag. For log off operation flag value is 0. private void btnLogOff_Click(object sender, EventArgs e)
{
ExitWindowsEx(0, 0);
}Forced Log off the System On click event of Forced Log Off button, call ExitWindowsEX method with proper flag. For Forced log off operation flag value is 4. private void btnForcedLogOff_Click(object sender, EventArgs e)
{
ExitWindowsEx(4, 0);
}Shut Down the SystemOn click event of Shut down button, call ExitWindowsEX method with proper flag. For shut down operation flag value is 1. private void btnShutDown_Click(object sender, EventArgs e)
{
ExitWindowsEx(1, 0);
}Restart the System On click event of Restart button, call ExitWindowsEX method with proper flag. For Restart operation flag value is 2. private void btnRestart_Click(object sender, EventArgs e)
{
ExitWindowsEx(2, 0);
}Now when you run the application all system operation should be performed. For your reference full source code is given here
Read more: C# Corner