禁用关闭时自动终止应用程序

Law*_*and 8 shutdown windows-xp

是否有任何 Windows XP 替代 Windows Vista 的关机过程提示用户是否继续或取消关机,以防某些程序包含未保存的数据?

Sco*_*ein 10

您可以通过处理SystemEvents.SessionEnding事件使用一些代码来完成此操作。当您尝试注销或关机并询问您是否要取消注销或关机时,这将显示一个对话框。

可以使用Visual C# 2008 Express EditionWindows SDK免费编译代码。

使用 sdk,使用以下命令:

csc.exe   /out:StopShutdown.exe /target:winexe StopShutdown.cs 
Run Code Online (Sandbox Code Playgroud)

这是代码:

using System;
using System.Windows.Forms;
using Microsoft.Win32;

namespace StopShutdown
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
           string desktopRegKey = @"HKEY_CURRENT_USER\Control Panel\Desktop";
           Registry.SetValue(desktopRegKey, "AutoEndTasks", 0);
           Registry.SetValue(desktopRegKey, "WaitToKillAppTimeout", 20000);
           Registry.SetValue(desktopRegKey, "HungAppTimeout", 20000);

            Form AppForm = new Form()
                {
                    ClientSize = new System.Drawing.Size(0, 0),
                    ControlBox = false,
                    FormBorderStyle = FormBorderStyle.None,
                    Opacity = 0,
                    ShowIcon = false,
                    ShowInTaskbar = false,
                    SizeGripStyle = SizeGripStyle.Hide,
                };

            SystemEvents.SessionEnding += (_e, e) =>
            {
                DialogResult dr = MessageBox.Show(
                                    "Cancel shutdown?"
                                    , "Shutdown",
                                    MessageBoxButtons.YesNo,
                                    MessageBoxIcon.Question,
                                    MessageBoxDefaultButton.Button1);

                e.Cancel = (dr == DialogResult.Yes);
            };


            Application.Run(AppForm);
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

可下载的源代码和 exe

  • 从 http://cid-552753bb951aae20.skydrive.live.com/self.aspx/.Public/Code%20Samples/StopShutdown.zip 下载 (2认同)