是否可以在不使用 pinvoke 的情况下使用 .NET 重新启动 PC?
我只是重复了标题,但我不太确定如何进一步详细说明!
编辑:
我应该提到不想使用“shutdown -r”作为解决方案。
我确实追求一种纯粹的 .NET 方式,例如:
Environment.ShutDown();
换句话说,随着新版本的 Windows 出现,由 .NET 维护的东西。
编辑2:
请停止询问“p/invoke 有什么问题”。这些类型的答案正是 SO 用户似乎喜欢的;所谓的“横向”方法来回答问题。然而,虽然 p/invoke 没有真正的问题,而且我很乐意使用它,但询问 .NET 是否有更官方的实现方法到底有什么问题呢?如果是在 .NET 中,那么操作系统之间的任何 API 更改都将(很可能)得到反映。不管出于什么原因,寻求最小化 DLL 导入的使用并不是犯罪,不是吗?
我确信如果我在一个问题中包含一些内容,例如:
[DllImport("something32.dll")]
static extern int ClimbWall32Ex(IntPtr32 blah);
你可以这样做:
SomeNamespace.ClimbWall();
这里的每个人都会尖叫:“使用 SomeNamespace.ClimbWall();
有什么问题吗??”
叹。
Is it possible, without employing pinvoke, to restart a PC using .NET?
I kind of just repeated the title, but I'm not too sure how to elaborate much further!
Edit:
I should have mentioned that don't want to use "shutdown -r" as a solution.
I was really after a pure .NET way, something like:
Environment.ShutDown();
In other words, something that is maintained with .NET as new versions of Windows arise.
Edit 2:
Please stop asking "what's wrong with p/invoke". Those sort of answers are exactly what SO users seem to love; the supposed "lateral" approach to answering a question. However, although there is no real problem with p/invoke and I will happily use it, what on earth is wrong with asking if .NET has a more official way of achieving something? If it's in .NET then any API changes between OSes will (most likely) get reflected. Whatever the reason, it's not a crime to seek to minimise DLL import usage is it?
I'm sure if I included something in a question like:
[DllImport("something32.dll")]
static extern int ClimbWall32Ex(IntPtr32 blah);
And you could just do:
SomeNamespace.ClimbWall();
Everyone one here would scream: "what is wrong with using SomeNamespace.ClimbWall();
??"
Sigh.
发布评论
评论(3)
不确定为什么不只使用 P/Invoke,但另一种重新启动方法是使用
System.Diagnostics.Process.Start
与关闭
命令。示例:
如果这也不可接受,您可以考虑使用 WMI (请参阅此处 是一个示例,可以根据您的目的进行修改) 。
Not sure why you wouldn't just use P/Invoke, but one alternate way of restarting would be to use
System.Diagnostics.Process.Start
in conjunction with theshutdown
command.Example:
If that also isn't acceptable, you could look into using WMI (see here for an example that could probably be modified to suit your purposes).
您可以使用 WMI 重新启动。下面是凭记忆写的,不过我觉得已经很接近了,虽然有点草率。 :)
You can use WMI to restart. Below is from memory, but I think it is pretty close, although a little sloppy. :)
您需要调用 ExitWindowsEx,它只能通过 DllImport 调用
You need to call ExitWindowsEx which is only available through DllImport