c# - 如何从 C#、Process.Start ("shutdown"关闭)在 Windows XP 中不起作用

标签 c# .net windows process.start

在研究了如何从 C# 中重置我的计算机和/或关闭它之后,我找到了关于如何执行此操作的解释:

ManagementBaseObject outParameters = null;
ManagementClass sysOS = new ManagementClass("Win32_OperatingSystem");
sysOS.Get();

// Enables required security privilege.
sysOS.Scope.Options.EnablePrivileges = true;

// Get our in parameters
ManagementBaseObject inParameters = sysOS.GetMethodParameters("Win32Shutdown");

// Pass the flag of 0 = System Shutdown
inParameters["Flags"] = "1"; //shut down.
inParameters["Reserved"] = "0";
foreach (ManagementObject manObj in sysOS.GetInstances())
{
    outParameters = manObj.InvokeMethod("Win32Shutdown", inParameters, null);
}

这在 Windows 7 中有效,但在我试过的 Windows XP 机器上无效。所以我认为我们可以采用更简单的解决方案:

Process.Start("shutdown", "/s /t 00");

唉,这似乎也适用于我的 Windows 7 机器,但不适用于我的 Windows XP 机器。我只在一台 Windows XP 机器上尝试过,但它像命令提示符一样闪烁,我正在运行的程序被最小化到系统托盘,然后什么也没有发生……所以它就像想做某事但最终什么也没有发生。 (我确实有代码在点击关闭 X 时故意将我的程序放入系统托盘,并且用户必须故意退出它......)这有问题吗?我的 FormClosing 代码是这样的:

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if (!canExit)
    {
        e.Cancel = true;
        this.WindowState = FormWindowState.Minimized;
    }
    else
    {
        // Write out the logs.
        List<String> logs = LogUtil.getLog(); // mic.getLog();

        // Create a writer and open the file
        TextWriter tw = new StreamWriter(userAppData + "\\logTMC.txt", true);

        // Write a line of text to the file
        tw.WriteLine("----- " + DateTime.Now + " ------");
        foreach (String log in logs)
        {
            tw.WriteLine(log);
        }

        // Close the stream
        tw.Close();
    }
}

我不确定为什么我可以在 Windows 7 中通过 C# 重置和关闭我的电脑,但在 Windows XP 中却不能...也许我错过了什么?一个额外的命令?关闭表单时关闭我打开的日志文件的更好方法?某种强制关机或重置的方法,无论如何,我使用的 Windows XP 盒子确实有一个 SVN 服务器作为 Windows 服务运行,但我不确定这是否有所作为。

所以我不太确定在哪里调查我的问题。 Process.Start() 是否有办法查看返回或 try catch 以查看可能导致它不关闭的原因,或者它是“即发即弃”类型的交易?

最佳答案

您可以通过 pinvoke.net 使用 ExitWindowsEx API。

参见 ExitWindowsEx , ExitWindows-EnumShutdownReason-Enum在 pinvoke.net 上获取更多信息。请注意,您的进程必须具有 SE_SHUTDOWN_NAME 权限(例如通过 AdjustTokenPrivileges API)。

this stackoverflow question 的答案包含一些“完整”示例(尽管其中大多数都缺少错误检查和资源清理 - 后者在您成功关闭时可能无关紧要,YMMV)。

最后,请注意,如您所示,使用 Process.Start() 时,如果没有 shutdown.exe 的完全限定名称,从安全角度来看也是有问题的。有人可能会在您的 PATH 中放置一个名为 shutdown 的恶意 EXE。由于您可能需要以管理员权限运行才能执行“真正的”shutdown.exe,这可能会造成一些麻烦。如果您指定类似 Process.Start(Environment.ExpandEnvironmentVariables("%windir%\system32\shutdown.exe")) 的内容,您至少可以假设真正的 shutdown.exe 受到保护,不会被恶意替换文件系统权限(如果攻击者本人是管理员,那么您基本上还是被破坏了)。

关于c# - 如何从 C#、Process.Start ("shutdown"关闭)在 Windows XP 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3724276/

相关文章:

c# - 从 System.Drawing.Bitmap 加载 WPF BitmapImage

c# - 我可以在接口(interface)上重载 == 运算符吗?

c# - 当我在 Entity Framework 中调用 Count 方法时,它是处理所有列还是只处理一个列或什么?

c# - String.format 无效

.net - 检测 Windows 机器是否正在运行病毒扫描程序?

c# - 在 C# 中比较两个任意长度的列表或数组;顺序很重要

c# - 将自定义创建的位图绘制到屏幕

c# - 无法将 Observable 集合绑定(bind)到 UserControl 上的附加属性

windows - 在 Windows Forms 窗体上动态添加控件

c# - EntityFramework 更新部分模型