The
e.Cancel = true;
will prevent your application from closing, but if someone tries to log off, only your application will prevent windows from logging off. You should note; it won't stop the closure of other applications on the system while the logoff is commencing. When windows shuts down, it collects a list of running applications in need of being closed before the log off can commence. Depending on the order of the opened applications being cued for closure, whatever applications are cued before yours, will also be closed regardless of your applications current state, and windows will only halt when your application is being processed. Any applications cued after your application will also not be shut down. However, a user can still persist to ignore your applications state and force the log off to continue by way of interacting with log off screen. See pic :
我认为完全防止关机的唯一方法是(可能的话)更改一些安全策略,以防止用户组具有手动注销的能力。这不是我以前尝试过的东西,但是有可能。
根据这个, there is an option to remove the button from the Start menu. So in theory, you could include
using Microsoft.GroupPolicy;
and get to work on rewriting the registry values. But yuck, and I would highly recommend you backup your registry in full before playing with a major part of the operating system if you're on a development machine or not. The Microsoft.GroupPolicy will give you much more control over a users ability to log off VIA the UI, but if they know how to reboot and log off via cmd, you may have a new problem on your hands to prevent that. Some here might argue that group policy might not be the best way to go about it.
〜添加缺少的链接