如何从C#的Process.Start关闭("关机")不工作在Windows XP [英] How to shut down from c#, Process.Start("shutdown") not working in windows XP

查看:239
本文介绍了如何从C#的Process.Start关闭("关机")不工作在Windows XP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一些关于如何重置我的电脑,或从C#将其关闭闲逛我发现怎么做这样的解释:

After some poking around on how to reset my computer and or shut it down from c# I found this explanation on how to do that:

        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中我尝试它。所以我也让带着一个简单的解决方案:

This worked in windows 7, but not on the windows XP box I tried it on. So I figured well lets go with a simpler solution:

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

唉,这似乎也工作在我的Windows 7中,但不是我的Windows XP中。我只是试图在一台Windows XP机器,但它闪烁像一个命令提示符下,我的程序是涨是最小化到系统托盘,然后就没有happens..so它像它想要做的东西,但最终什么也没有发生。 (我有code,它故意把我的程序到SYS托盘当关闭X被击中,并且用户具有故意退出吧...)有一个问题是什么?我的FormClosing code是这样的:

Alas that as well seems to work on my windows 7 box, but not my windows XP box. I have only tried it on one windows XP machine, but it flashes up like a command prompt, my program that is up is minimized to the system tray and then nothing happens..so its like it wants to do something but ultimately nothing happens. (I do have code that purposely puts my program to the sys tray when the close X is hit, and the user has to purposely exit it... ) is there an issue with that? My FormClosing code is this:

    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#,但不能在XP中...也许我错过了什么?一个额外的命令?更好的方式来收出日志文件我已打开的窗体关闭的时候?某种方式来强制关机或重启不管是什么,我现在用的确实有一个SVN服务器作为Windows服务运行在XP中,不知道这是否有差别或没有。

I am not sure why I can reset, and shutdown my pc from C# in windows 7 but not on XP...maybe I missed something? An extra command? a better way to close out the log file I have open when the form closes? some way to force a shutdown or reset no matter what, the xp box I am using does indeed have a SVN server as a windows service running, not sure if this makes a difference or not.

所以,真的不知道哪里来调查我的问题,请问的Process.Start有办法看一回或尝试捕捉,看看有什么会引起它不关机或者是一种发射后不管型说定了?

So not really sure where to investigate my problem, does the process.start have a way to see a return or a try catch to see what might of caused it not to shut down or is it a "fire and forget" type a deal?

推荐答案

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

You could use the ExitWindowsEx API via pinvoke.net.

请参见 ExitWindowsEx ,的 ExitWindows-枚举和的 ShutdownReason-枚举上pinvoke.net以获取更多信息。请注意,您的进程必须具有 SE_SHUTDOWN_NAME 特权获得性(例如,通过的 AdjustTokenPrivileges API)。

See the ExitWindowsEx, ExitWindows-Enum and ShutdownReason-Enum on pinvoke.net for more information. Note that your process must have the SE_SHUTDOWN_NAME priviledge aquired (for example via AdjustTokenPrivileges API).

这个计算器问题包含一些完整的答案例子。(虽然他们大多缺少错误检查和资源清理 - 后者可能没有关系,当你成功关闭,情况因人而异)

The answers to this stackoverflow question contain some "complete" examples (although most of them are missing errorchecking and resource cleanup - the latter might not matter when you successfully shutdown, YMMV).

最后,请注意,使用的Process.Start()为您呈现,没有一个完全合格的名称 shutdown.exe的也是从安全角度来看问题。有人可以把一个恶意的EXE名为关闭在路径中。因为你很可能需要以管理员权限运行,以便能够执行真正的Shutdown.exe的,这可能会导致一些麻烦。如果指定了类似的Process.Start(Environment.ExpandEnvironmentVariables(%WINDIR%\ SYSTEM32 \ shutdown.exe的)),你至少可以认为,真正的shutdown.exe的免受恶意置换文件系统权限(如果攻击者本人是一个管理你反正基本上捣毁)。

Finally, note that using Process.Start() as you showed, without a fully qualified name to shutdown.exe is also problematic from a security standpoint. Someone could put a malicious EXE named shutdown in your PATH. Since you probably need to run with admin rights to be able to execute the "real" shutdown.exe, this can cause some trouble. If you specify something like Process.Start(Environment.ExpandEnvironmentVariables("%windir%\system32\shutdown.exe")) you can at least assume that the real shutdown.exe is protected from malicious replacement by file system rights (if the attacker himself is an admin your basically busted anyway).

这篇关于如何从C#的Process.Start关闭(&QUOT;关机&QUOT;)不工作在Windows XP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆