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
2009-01-20 18:07:05
+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
2009-01-20 18:07:54
+1 Simple solution for what could have been a complex question.
Sung Meister
2009-04-04 18:12:14
+4
A:
I don't know a pure .NET way to do it. Your options include:
- P/Invoke the ExitWindowsEx Win32 function
- Use Process.Start to run shutdown.exe as already suggested.
driis
2009-01-20 18:09:18
+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
2009-01-20 18:09:22
A:
check this aticle.. that gives the answers for ur question
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
2009-01-20 18:14:12