How to shutdown the system programmatically using C#?.
The articles explain how to shutdown the system pro-grammatically even if we don't have admin rights.Always we are working in a system connected with LAN Server. Normally they won't give admin rights to our system.
The articles explain how to shutdown the system pro-grammatically even if we don't have admin rights.
Normally in C# , we can use Process class to start,stop application with arguments.
Example to shutdown the system look like this
System.Diagnostics.Process.Start("Shutdown", "-s -f -t 60");
Process class will start the shutdown exe with parameters. if we execute the above code , will get error if you don't have admin rights, to overcome that error we need to use interopservices class.
public class logoff
{
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential, Pack = 1)]
internal struct AccessPriv1lage
{
public int iCnt;
public long Lid;
public int Attr;
}
[System.Runtime.InteropServices.DllImport("kernel32.dll", ExactSpelling = true)]
internal static extern IntPtr GtCrrentPrcessList();
[System.Runtime.InteropServices.DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool TokenList(IntPtr h, int acc, ref IntPtr phtok);
[System.Runtime.InteropServices.DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool PrivilegeValue(string host, string name, ref long pluid);
[System.Runtime.InteropServices.DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool TokenPriviligesSet(IntPtr htok, bool disall,
ref AccessPriv1lage newst, int len, IntPtr prev, IntPtr relen);
[System.Runtime.InteropServices.DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool StopWindows(int flg, int rea);
internal const int HexaCodeENABLED = 0x00000002;
internal const int HexaQUERY = 0x00000008;
internal const int Hexaadjuest = 0x00000020;
internal const string strshutdownprivili = "SeShutdownPrivilege";
internal const int hexaforce = 0x00000004;
internal const int hexapower = 0x00000008;
private static void shutPC(int statusflg)
{
bool boolstatus;
AccessPriv1lage objAccessPrivilage;
IntPtr ProcessListpointer = GtCrrentPrcessList();
IntPtr PointerforZero = IntPtr.Zero;
boolstatus = TokenList(ProcessListpointer, Hexaadjuest | HexaQUERY, ref PointerforZero);
objAccessPrivilage.iCnt = 1;
objAccessPrivilage.Lid = 0;
objAccessPrivilage.Attr = HexaCodeENABLED;
boolstatus = PrivilegeValue(null, strshutdownprivili, ref objAccessPrivilage.Lid);
boolstatus = TokenPriviligesSet(PointerforZero, false, ref objAccessPrivilage, 0, IntPtr.Zero, IntPtr.Zero);
boolstatus = StopWindows(statusflg, 0);
}
public static void ExitWindowsForcefully()
{
shutPC(hexapower | hexaforce);
}
}
The above code System.Runtime.InteropServices.DllImport attribute,, it will exposed unmanaged dynamically library.
create instance for the class and call the ExitWindowsForcefully function
logoff lf=new logoff();
lf.ExitWindowsForcefully();