如何关闭计算机?

发布于 2024-07-15 13:03:17 字数 21 浏览 11 评论 0原文

如何使用 C# 关闭计算机?

How to shutdown my computer using C#?

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

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

发布评论

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

评论(7

后知后觉 2024-07-22 13:03:17

一个简单的方法:使用 Process.Start 运行 shutdown.exe。

  shutdown /s /t 0

编程方式:P/Invoke 调用 ExitWindowsEx

这将是 P/Invoke 签名:

[DllImport("aygshell.dll", SetLastError="true")]
private static extern bool ExitWindowsEx(uint dwFlags, uint dwReserved);

在所有情况下,运行代码的用户都需要关闭系统权限(通常不是问题,但要记住这一点)。

An easy way: Use Process.Start to run shutdown.exe.

  shutdown /s /t 0

Programmatic way: P/Invoke a call to ExitWindowsEx

This would be the P/Invoke signature:

[DllImport("aygshell.dll", SetLastError="true")]
private static extern bool ExitWindowsEx(uint dwFlags, uint dwReserved);

Under all circumstances, the user running the code will need shutdown system privileges (normally not a problem, but an important point to remember).

巾帼英雄 2024-07-22 13:03:17

不同的方法:

A. System.Diagnostics.Process.Start("Shutdown", "-s -t 10");

B. Windows Management Instrumentation (WMI)

http://www.csharpfriends.com/ Forums/ShowPost.aspx?PostID=36953

http://www.dreamincode.net /forums/showtopic33948.htm

C. System.Runtime.InteropServices Pinvoke

http://bytes.com/groups/net-c/251367-shutdown-my-computer-using-c

D.系统管理

http://www.geekpedia.com/code36_Shut-down-system-using-Csharp.html

之后提交,我看到很多人也发帖了...

Different methods:

A. System.Diagnostics.Process.Start("Shutdown", "-s -t 10");

B. Windows Management Instrumentation (WMI)

http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=36953

http://www.dreamincode.net/forums/showtopic33948.htm

C. System.Runtime.InteropServices Pinvoke

http://bytes.com/groups/net-c/251367-shutdown-my-computer-using-c

D. System Management

http://www.geekpedia.com/code36_Shut-down-system-using-Csharp.html

After I submit, I have seen so many others also have posted...

梦年海沫深 2024-07-22 13:03:17

WindowsController 是 ExitWindowsEx 的 ac# 包装类。

有时您需要重新启动或
关闭操作系统
您的申请(例如,之后
程序的安装)。 这
.NET框架为您提供了一种间接的
重新启动计算机的方法是通过
Windows管理工具
System.Management 中的 (WMI) 类
命名空间,但是,似乎有
实施中存在一些问题。

这就是我们创建
WindowsController 类
实现一些API函数
重新启动和关闭 Windows。 它
支持所有 ExitWindowsEx 模式
它还可以休眠和挂起
系统。

此类可用于 C# 和
VB.NET版本。 它可以编译为
.NET 模块或库
从其他 .NET 语言使用。 自从
它依赖于 Windows API,它将
不适用于 Linux 或 FreeBSD。

(mentalis.org)

WindowsController is a c# wrapper class around ExitWindowsEx.

Sometimes you need to restart or
shutdown the operating system from
your applications (for instance, after
the installation of a program). The
.NET framework offers you an indirect
way to restart the computer through
the Windows Management Instrumentation
(WMI) classes in the System.Management
namespace, however, there seem to be
some problems in their implementation.

That's why we created the
WindowsController class that
implements some API functions to
restart and shutdown Windows. It
supports all the ExitWindowsEx modes
and it can also hibernate and suspend
the system.

This class is available in C# and
VB.NET version. It can be compiled to
a .NET module or to a library to be
used from other .NET languages. Since
it relies on the Windows API, it will
not work on Linux or FreeBSD.

(mentalis.org)

埖埖迣鎅 2024-07-22 13:03:17

使用此处显示的“用户注销”代码的变体

该代码使用 ExitWindowsEx API 调用。

Use a variation of the "user logoff" code shown here.

That code uses the ExitWindowsEx API call.

赠我空喜 2024-07-22 13:03:17

猜测(未经测试):

Process.Start("shutdown", "-s -t 0");

At a guess (untested):

Process.Start("shutdown", "-s -t 0");
镜花水月 2024-07-22 13:03:17

困难的方法是在笔记本电脑上完美运行,尽管需要一些时间:

Spawn a couple endless loops in more threads than cpu cores.
Wait for overheat which will automatically shutdown a computer.

:)

The hard way, works on laptops perfectly, although it takes some time:

Spawn a couple endless loops in more threads than cpu cores.
Wait for overheat which will automatically shutdown a computer.

:)

贪恋 2024-07-22 13:03:17

您还可以使用 InitiateSystemShutdown

http://www.pinvoke.net/default.aspx/advapi32。启动系统关闭

using System;
using System.Runtime.InteropServices;
using System.Text;

public class Program
{
    [DllImport( "advapi32.dll" ) ]
    public static extern bool InitiateSystemShutdown( string MachineName , string Message , uint Timeout , bool AppsClosed , bool Restart );

    [DllImport( "kernel32.dll" ) ]
    public static extern uint GetLastError();

    [DllImport( "kernel32.dll" ) ]
    public static extern uint FormatMessage( uint Flags , IntPtr Source , uint MessageID , uint LanguageID , StringBuilder Buffer , uint Size , IntPtr Args );



    public static void Main()
    {
        InitiateSystemShutdown(System.Environment.MachineName, "hello", 0, false, false);
        //InitiateSystemShutdown("localhost", "hello", 0, false, false);

    }
}

You could also use InitiateSystemShutdown

http://www.pinvoke.net/default.aspx/advapi32.initiatesystemshutdown

using System;
using System.Runtime.InteropServices;
using System.Text;

public class Program
{
    [DllImport( "advapi32.dll" ) ]
    public static extern bool InitiateSystemShutdown( string MachineName , string Message , uint Timeout , bool AppsClosed , bool Restart );

    [DllImport( "kernel32.dll" ) ]
    public static extern uint GetLastError();

    [DllImport( "kernel32.dll" ) ]
    public static extern uint FormatMessage( uint Flags , IntPtr Source , uint MessageID , uint LanguageID , StringBuilder Buffer , uint Size , IntPtr Args );



    public static void Main()
    {
        InitiateSystemShutdown(System.Environment.MachineName, "hello", 0, false, false);
        //InitiateSystemShutdown("localhost", "hello", 0, false, false);

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