从.NET应用程序中重新启动Windows [英] Restarting Windows from within a .NET application

查看:45
本文介绍了从.NET应用程序中重新启动Windows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用.NET框架重新启动或关闭Windows?

How could I restart or shutdown Windows using the .NET framework?

推荐答案

以下代码将从外壳程序执行shutdown命令:

The following code will execute the shutdown command from the shell:

// using System.Diagnostics;

class Shutdown
{
    /// <summary>
    /// Windows restart
    /// </summary>
    public static void Restart()
    {
        StartShutDown("-f -r -t 5");
    }

    /// <summary>
    /// Log off.
    /// </summary>
    public static void LogOff()
    {
        StartShutDown("-l");
    }

    /// <summary>
    ///  Shutting Down Windows 
    /// </summary>
    public static void Shut()
    {
        StartShutDown("-f -s -t 5");
    }

    private static void StartShutDown(string param)
    {
        ProcessStartInfo proc = new ProcessStartInfo();
        proc.FileName = "cmd";
        proc.WindowStyle = ProcessWindowStyle.Hidden;
        proc.Arguments = "/C shutdown " + param;
        Process.Start(proc);
    }
}

(来源: http://dotnet-snippets.de/dns/c-windows-herrunterfahren-ausloggen-neustarten-SID455.aspx

这篇关于从.NET应用程序中重新启动Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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