That’s it now your PC will be shut down automatically after 1800 seconds (30 minutes).Type the “ shutdown.exe –s –f –t 1800” or “shutdown.exe –s –t 1800” (without quotes) press enter button and close the command prompt.First of all open CMD (Command Prompt): Click on windows menu button, search for CMD and open it or press Windows key + R, type cmd and hit enter button to open it.Method 2: Creating A BAT File On Windows PCĪuto Shutdown Time is one of the simplest method to shut down your pc at a specific time without any third party software.Method 2: Auto Shut Down By Task Scheduler.Private void btnShutDown_Click(object sender, EventArgs e) Remember to add a reference to the System.Management assembly TOKEN_PRIVILEGES tokp = (TOKEN_PRIVILEGES)Marshal.PtrToStructure(tu, successful AdjustTokenPrivileges doesn't mean privilege could be changed If (AdjustTokenPrivileges(hToken, 0, tu, bufLength, IntPtr.Zero, ref ltkpOld) != 0) IntPtr tu = Marshal.AllocHGlobal(bufLength) If (OpenProcessToken(proc.Handle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, Process proc = Process.GetCurrentProcess() If (LookupPrivilegeValue(null, lpszPrivilege, ref tLUID)) TOKEN_PRIVILEGES tkpOld = new TOKEN_PRIVILEGES() TOKEN_PRIVILEGES tkp = new TOKEN_PRIVILEGES() Public static bool EnablePrivilege(string lpszPrivilege, bool
ANYSIZE_ARRAY = 1Ĭonst int SE_PRIVILEGE_ENABLED = 0x00000002 Ĭonst int TOKEN_ADJUST_PRIVILEGES = 0X00000020 Ĭonst int PROCESS_QUERY_INFORMATION = 0X00000400 Static extern bool CloseHandle(IntPtr handle) [DllImport("kernel32", SetLastError = true), Ref IntPtr TokenHandle // handle to open access token Int DesiredAccess, // desired access to process System.IntPtr ProcessHandle, // handle to process [DllImport("advapi32", SetLastError = true), PInvoke stuff required to set/enable security privileges MboShutdown = manObj.InvokeMethod("Win32Shutdown", Flag 1 means we want to shut down the systemįoreach (ManagementObject manObj in mcWin32.GetInstances()) ManagementBaseObject mboShutdownParams = mcWin32.GetMethodParameters("Win32Shutdown") You can't shutdown without security privileges If (!TokenAdjuster.EnablePrivilege("SeShutdownPrivilege", true))Ĭonsole.WriteLine("Could not enable SeShutdownPrivilege") Ĭonsole.WriteLine("Enabled SeShutdownPrivilege") ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem") I've pasted my basic use of his solution below in case that link gets old. I found the answer at written by a guy called Richard Hill. The solution was for the process to request the privilege for itself. I had trouble trying to use the WMI method accepted above because i always got privilige not held exceptions despite running the program as an administrator.
#HOW TO MAKE SHUTDOWN TIMER WINDOWS 10 CODE#
In production code you should be checking the return values of the API calls, but I left that out to make the example clearer. Static extern int ExitWindowsEx(uint uFlags, uint dwReason) Static extern int LookupPrivilegeValue(string lpSystemName, Static extern bool AdjustTokenPrivileges(IntPtr TokenHandle,īool DisableAllPrivileges, Int DesiredAccess, out IntPtr TokenHandle) Static extern int OpenProcessToken(IntPtr ProcessHandle, Now we have the privileges, shutdown Windows LookupPrivilegeValue("", SE_SHUTDOWN_NAME, out ) ĪdjustTokenPrivileges(hToken, false, ref tkp, 0U, IntPtr.Zero, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, out hToken) OpenProcessToken(Process.GetCurrentProcess().Handle, using Ĭonst string SE_SHUTDOWN_NAME = "SeShutdownPrivilege" Ĭonst short TOKEN_ADJUST_PRIVILEGES = 32 Use the ExitWindowsEx function from the Win32 API. Ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero )
Ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid ) Ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok ) Internal const int EWX_FORCEIFHUNG = 0x00000010 Internal const int EWX_POWEROFF = 0x00000008 Internal const int EWX_FORCE = 0x00000004 Internal const int EWX_REBOOT = 0x00000002 Internal const int EWX_SHUTDOWN = 0x00000001 Internal const int EWX_LOGOFF = 0x00000000 Internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege" Internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020 Internal const int TOKEN_QUERY = 0x00000008 Internal const int SE_PRIVILEGE_ENABLED = 0x00000002 Internal static extern bool ExitWindowsEx( int flg, int rea ) Ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen ) Internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall, Internal static extern bool LookupPrivilegeValue( string host, string name, Internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr Internal static extern IntPtr GetCurrentProcess() This thread provides the code necessary: īut here's the relevant code: using