How to programmatically call "Shut down", "Restart", "Update and Shut down", "Update and restart" from WinUI 3 C# app?

1 day ago 1
ARTICLE AD BOX

I want to call the start menu options ("Shut down", "Restart", "Update and shut down", and "Update and restart") from my WinUI 3 C# app (using Visual Studio).

I tried Shutdown by calling: shutdown.exe /s /hybrid /t 0

I feel it is correct; however, I am not sure if this is what to call. I also tried Update and Shut Down by calling:

[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern bool InitiateShutdown(string lpMachineName, string lpMessage, uint dwGracePeriod, uint dwShutdownFlags, uint dwReason); InitiateShutdown(null, null, 0, SHUTDOWN_POWEROFF | SHUTDOWN_INSTALL_UPDATES | SHUTDOWN_FORCE_OTHERS, SHTDN_REASON_FLAG_PLANNED);

It called update and then shut down; however, Windows 11's Update and Shut Down triggers a restart so that the PC restarts, does the remaining update, and then shuts down. This ensures that when the PC opens, we don't have to wait for more updates.

Hence, I want the proper method to invoke those 4 options.

Read Entire Article