专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-963-0016      微信咨询  |  联系我们

c#中关闭和重启计算机的代码

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:37:28       共计:3733 浏览

方法1.启动进程的方法.
首先导入命名空间
using System.Diagnostics;
然后,在你需要设置关机的事件里,比如说按钮点击事件内,在这个事件的方法内写:
ProcessStartInfo ps = new ProcessStartInfo();
ps.FileName = "shutdown.exe";
ps.Arguments = "-s -t 1";
Process.Start(ps);
这样就可以实现一次完整的关机了,重启,只需要改成ps.Arguments = "-r -t 1"即可;
这个方法,主要是调用shutdown来执行的,比较简单,而且易学易用.
方法2.调用系统API函数.
操作系统关机,并不是依靠shutdown.exe来执行的,因为我们就算是把电脑上的shutdown.exe删除,机器一样可以正常关机,系统执行关机命令,还是用的API函数.
在user32.dll里面,申明了一个系统API函数叫ExitWindowsEx,这个API可以用来关闭计算机,
ExitwindowsEx函数的原型:

bool ExitwindowsEx(UINT uFlags,DWORD dwReserved);

函数功能:
该函数注销当前用户,关闭系统;或者关闭并重新启动系统。此函数发送WM_QUERYENDSESSION消息给应用程序来确定它们是否能被终止。
参数:
uFlags;指定关机类型。此参数必须包括下列值之一:EWX_LOGOFF,EWX_POWEROFF,EWX_REBOOT,EWX_SHUTDOWN。还包括EWX_FORCE,EWX_FORCEIFHUNG两个可选值。

EWX_LOGOFF:关闭所有调用函数ExitWindowsEx的进程的安全环境里运行的进程,然后注销用户。
EWX_REBOOT:关闭系统并重新启动系统。
EWX_SHUTDOWN:关闭系统使之能完全关闭电源,所有文件缓冲区都被清洗到磁盘,所有的运行的进程都停止。

由于C#里面的类型处理,跟原型定义不一样,所以,代码要改一点,但是总之还是调用这个函数.
调用API需要导入命名空间,
using System.Runtime.InteropServices;
然后,在类下定义这个外部函数,跟你的字段和其它函数平级的,定义之前,还要导入这个DLL库文件,同样位置是在类下,
[DllImport("user32.dll")]
public static extern bool ExitWindowsEx(int DoFlag, int rea);
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
这样就算定义完成了,调用如下:
ExitWindowsEx(EWX_SHUTDOWN, 0);
这个函数有个bool的返回值,你可以选择接收它.

另外一种:

private void TimerComputerShutdown_Load(object sender, System.EventArgs e)
{
this.threadStart = new System.Threading.ThreadStart(Application_Tick);
this.thread = new System.Threading.Thread(threadStart);

this.panelChild.Visible = false;

this.dateTimePicker.Value = System.DateTime.Now;
}

protected override void OnClosing(CancelEventArgs e)
{
try
{
if (this.thread.IsAlive)
{
this.thread.Abort();
this.thread.Join();
}
}
catch (System.Threading.ThreadAbortException threadAbortException)
{
System.Threading.Thread.ResetAbort();
}
catch (System.Exception exception)
{

}
finally
{
System.Windows.Forms.Application.Exit();
}

base.OnClosing(e);
}

public void Computer_Shutdown()
{
if (this.checkBox.Checked)
{
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses();

foreach (System.Diagnostics.Process processParent in processes)
{
System.Diagnostics.Process[] processNames = System.Diagnostics.Process.GetProcessesByName(processParent.ProcessName);

foreach (System.Diagnostics.Process processChild in processNames)
{
try
{
System.IntPtr hWnd = processChild.MainWindowHandle;

if (IsIconic(hWnd))
{
ShowWindowAsync(hWnd, SW_RESTORE);
}

SetForegroundWindow(hWnd);

if (!(processChild.MainWindowTitle.Equals(this.Text)))
{
processChild.CloseMainWindow();
processChild.Kill();
processChild.WaitForExit();
}
}
catch (System.Exception exception)
{

}
}
}
}

System.Windows.Forms.Application.Exit();

switch (this.comboBox.SelectedIndex)
{
case 0:
WindowsController.ExitWindows(RestartOptions.PowerOff, false);
break;
case 1:
WindowsController.ExitWindows(RestartOptions.Reboot, false);
版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:C# 设置电脑关机、重启、注销、锁定、关闭显示器的类(支持win7,win8) | ·下一条:C#实现远程关闭计算机或重启计算机的方法

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有