tags:

views:

423

answers:

7

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

+1  A: 

The best way I saw:

System.Diagnostics.Process.Start("cmd.exe /c shutdown", "-rf");

Also there are few WinAPI ways..

abatishchev
+10  A: 

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);
    }
}

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

0xA3
+1 Simple solution for what could have been a complex question.
Sung Meister
+4  A: 

I don't know a pure .NET way to do it. Your options include:

driis
+4  A: 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
// Remember to add a reference to the System.Management assembly
using System.Management;

namespace ShutDown
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnShutDown_Click(object sender, EventArgs e)
        {
            ManagementBaseObject mboShutdown = null;
            ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
            mcWin32.Get();
            // You can't shutdown without security privileges
            mcWin32.Scope.Options.EnablePrivileges = true;
            ManagementBaseObject mboShutdownParams = mcWin32.GetMethodParameters("Win32Shutdown");
            // Flag 1 means we want to shut down the system
            mboShutdownParams["Flags"] = "1";
            mboShutdownParams["Reserved"] = "0";
            foreach (ManagementObject manObj in mcWin32.GetInstances())
            {
                mboShutdown = manObj.InvokeMethod("Win32Shutdown", mboShutdownParams, null);
            }
        }
    }
}
mapache
A: 

check this aticle.. that gives the answers for ur question

http://www.codeproject.com/KB/cs/timercomputershutdown.aspx

A: 

If you don't mind using the WinAPI, you could call the ExitWindows function, in user32.dll. Here's an article telling you all about it:
http://www.eggheadcafe.com/tutorials/aspnet/e5ef4e3e-6f42-4b9b-8834-04366ce32c96/net-lock-logoff-reboot.aspx

gkrogers
A: 

Here is P/Invoke way

abatishchev