Code for Shutdown,Restart and Logoff....
--------------------------------------------
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;
namespace clikme{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } [DllImport("user32.dll", SetLastError = true)] static extern int ExitWindowsEx(uint uFlags, uint dwReason); enum ExitFlags { Logoff = 0, Shutdown = 1, Reboot = 2, Force = 4, PowerOff = 8, ForceIfHung = 16 } enum Reason : uint { ApplicationIssue = 0x00040000, HardwareIssue = 0x00010000, SoftwareIssue = 0x00030000, PlannedShutdown = 0x80000000 } const int PrivilegeEnabled = 0x00000002; const int TokenQuery = 0x00000008; const int AdjustPrivileges = 0x00000020; const string ShutdownPrivilege = "SeShutdownPrivilege";
[StructLayout(LayoutKind.Sequential, Pack = 1)] internal struct TokenPrivileges { public int PrivilegeCount; public long Luid; public int Attributes; }
[DllImport("kernel32.dll")] internal static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", SetLastError = true)] internal static extern int OpenProcessToken( IntPtr processHandle, int desiredAccess, ref IntPtr tokenHandle);
[DllImport("advapi32.dll", SetLastError = true)] internal static extern int LookupPrivilegeValue( string systemName, string name, ref long luid);
[DllImport("advapi32.dll", SetLastError = true)] internal static extern int AdjustTokenPrivileges( IntPtr tokenHandle, bool disableAllPrivileges, ref TokenPrivileges newState, int bufferLength, IntPtr previousState, IntPtr length); private void ElevatePrivileges() { IntPtr currentProcess = GetCurrentProcess(); IntPtr tokenHandle = IntPtr.Zero;
int result = OpenProcessToken( currentProcess, AdjustPrivileges | TokenQuery, ref tokenHandle);
if (result == 0) throw new Win32Exception(Marshal.GetLastWin32Error());
TokenPrivileges tokenPrivileges; tokenPrivileges.PrivilegeCount = 1; tokenPrivileges.Luid = 0; tokenPrivileges.Attributes = PrivilegeEnabled;
result = LookupPrivilegeValue( null, ShutdownPrivilege, ref tokenPrivileges.Luid);
result = AdjustTokenPrivileges( tokenHandle, false, ref tokenPrivileges, 0, IntPtr.Zero, IntPtr.Zero);
if (result == 0) throw new Win32Exception(Marshal.GetLastWin32Error()); } private void Form1_Load(object sender, EventArgs e) {
}
private void ShutdownButton_Click(object sender, EventArgs e) { MessageBox.Show("The System is going to be Shutdown in few minutes...."); ElevatePrivileges();
int result = ExitWindowsEx( (uint)(ExitFlags.Shutdown | ExitFlags.PowerOff | ExitFlags.Force), (uint)(Reason.HardwareIssue | Reason.PlannedShutdown));
if (result == 0) throw new Win32Exception(Marshal.GetLastWin32Error()); }
private void RestartButton_Click(object sender, EventArgs e) { MessageBox.Show("The System is going to be Restart in few minutes...."); ElevatePrivileges();
int result = ExitWindowsEx( (uint)(ExitFlags.Reboot), (uint)(Reason.SoftwareIssue | Reason.PlannedShutdown));
if (result == 0)  
you can also call windows API for shutdown and restart. [DllImport("user32.dll")] public static extern int ExitWindowsEx(int uFlags, int dwReason); Shutdown - ExitWindowsEx(1, 0) Restart - ExitWindowsEx(2, 0);
Add this NameSpace Using System.Diagnostics Try this code System.Diagnostics.Process.Start("ShutDown", "/r") ---------------------------- -s Shutdown the computer -r Restart the computer
using System;
using
namespace
{
p.Kill();
// write this code to your program.cs file.This code not close all the files automatically(not proper) but it urged you to close and save all files with in 1 miniutes.