是否可以使用“纯”重启电脑? .NET 和*不*使用 p/invoke?

发布于 2024-08-07 13:01:14 字数 783 浏览 7 评论 0 原文

是否可以在不使用 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

皓月长歌 2024-08-14 13:01:14

不确定为什么不只使用 P/Invoke,但另一种重新启动方法是使用 System.Diagnostics.Process.Start关闭命令。

示例:

System.Diagnostics.Process.Start("shutdown", "-r");

如果这也不可接受,您可以考虑使用 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 the shutdown command.

Example:

System.Diagnostics.Process.Start("shutdown", "-r");

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).

念三年u 2024-08-14 13:01:14

您可以使用 WMI 重新启动。下面是凭记忆写的,不过我觉得已经很接近了,虽然有点草率。 :)

var computer = "COMPUTERNAME";
var query = string.Format("SELECT * FROM Win32_OperatingSystem");

ManagementScope scope;

var computerPath = string.Format(@"\\{0}\root\cimv2", computer);

scope = new ManagementScope(computerPath);

scope.Connect();

var q = new ObjectQuery(query);
var s = new ManagementObjectSearcher(scope, q);

ManagementObjectCollection qr;

qr = s.Get();

foreach (ManagementObject r in qr) 
{
    string[] p = { "" };
    r.InvokeMethod("Reboot", p);
}

You can use WMI to restart. Below is from memory, but I think it is pretty close, although a little sloppy. :)

var computer = "COMPUTERNAME";
var query = string.Format("SELECT * FROM Win32_OperatingSystem");

ManagementScope scope;

var computerPath = string.Format(@"\\{0}\root\cimv2", computer);

scope = new ManagementScope(computerPath);

scope.Connect();

var q = new ObjectQuery(query);
var s = new ManagementObjectSearcher(scope, q);

ManagementObjectCollection qr;

qr = s.Get();

foreach (ManagementObject r in qr) 
{
    string[] p = { "" };
    r.InvokeMethod("Reboot", p);
}
小女人ら 2024-08-14 13:01:14

您需要调用 ExitWindowsEx,它只能通过 DllImport 调用

You need to call ExitWindowsEx which is only available through DllImport

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文