जवाबों:
Windows XP के साथ काम करना, 2000 या उससे कम जीत में उपलब्ध नहीं:
यह इसे करने का सबसे तेज़ तरीका है:
Process.Start("shutdown","/s /t 0");
अन्यथा पी / इनवोक या डब्ल्यूएमआई का उपयोग करें जैसे अन्य ने कहा है।
संपादित करें: विंडो बनाने से कैसे बचें
var psi = new ProcessStartInfo("shutdown","/s /t 0");
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
Process.Start(psi);
से लिया गया: एक Geekpedia पोस्ट
यह विधि विंडोज़ को बंद करने के लिए WMI का उपयोग करती है ।
इसका उपयोग करने के लिए आपको अपने प्रोजेक्ट में System.Management का संदर्भ जोड़ना होगा।
using System.Management;
void Shutdown()
{
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. Use "2" to reboot.
mboShutdownParams["Flags"] = "1";
mboShutdownParams["Reserved"] = "0";
foreach (ManagementObject manObj in mcWin32.GetInstances())
{
mboShutdown = manObj.InvokeMethod("Win32Shutdown",
mboShutdownParams, null);
}
}
यह धागा आवश्यक कोड प्रदान करता है: http://bytes.com/forum/thread251367.html
लेकिन यहां प्रासंगिक कोड है:
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}
[DllImport("kernel32.dll", ExactSpelling=true) ]
internal static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr
phtok );
[DllImport("advapi32.dll", SetLastError=true) ]
internal static extern bool LookupPrivilegeValue( string host, string name,
ref long pluid );
[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen );
[DllImport("user32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool ExitWindowsEx( int flg, int rea );
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
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;
internal const int EWX_FORCEIFHUNG = 0x00000010;
private void DoExitWin( int flg )
{
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok );
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid );
ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero );
ok = ExitWindowsEx( flg, 0 );
}
उपयोग:
DoExitWin( EWX_SHUTDOWN );
या
DoExitWin( EWX_REBOOT );
विभिन्न तरीकों:
ए। System.Diagnostics.Process.Start("Shutdown", "-s -t 10");
B. विंडोज मैनेजमेंट इंस्ट्रूमेंटेशन (WMI)
C. System.Runtime.InteropServices Pinvoke
डी। सिस्टम प्रबंधन
जमा करने के बाद, मैंने देखा है कि कई अन्य लोगों ने भी पोस्ट किया है ...
पुराने स्कूल बदसूरत विधि। ExitWindowsEx
Win32 API से फ़ंक्शन का उपयोग करें ।
using System.Runtime.InteropServices;
void Shutdown2()
{
const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
const short SE_PRIVILEGE_ENABLED = 2;
const uint EWX_SHUTDOWN = 1;
const short TOKEN_ADJUST_PRIVILEGES = 32;
const short TOKEN_QUERY = 8;
IntPtr hToken;
TOKEN_PRIVILEGES tkp;
// Get shutdown privileges...
OpenProcessToken(Process.GetCurrentProcess().Handle,
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, out hToken);
tkp.PrivilegeCount = 1;
tkp.Privileges.Attributes = SE_PRIVILEGE_ENABLED;
LookupPrivilegeValue("", SE_SHUTDOWN_NAME, out tkp.Privileges.pLuid);
AdjustTokenPrivileges(hToken, false, ref tkp, 0U, IntPtr.Zero,
IntPtr.Zero);
// Now we have the privileges, shutdown Windows
ExitWindowsEx(EWX_SHUTDOWN, 0);
}
// Structures needed for the API calls
private struct LUID
{
public int LowPart;
public int HighPart;
}
private struct LUID_AND_ATTRIBUTES
{
public LUID pLuid;
public int Attributes;
}
private struct TOKEN_PRIVILEGES
{
public int PrivilegeCount;
public LUID_AND_ATTRIBUTES Privileges;
}
[DllImport("advapi32.dll")]
static extern int OpenProcessToken(IntPtr ProcessHandle,
int DesiredAccess, out IntPtr TokenHandle);
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AdjustTokenPrivileges(IntPtr TokenHandle,
[MarshalAs(UnmanagedType.Bool)]bool DisableAllPrivileges,
ref TOKEN_PRIVILEGES NewState,
UInt32 BufferLength,
IntPtr PreviousState,
IntPtr ReturnLength);
[DllImport("advapi32.dll")]
static extern int LookupPrivilegeValue(string lpSystemName,
string lpName, out LUID lpLuid);
[DllImport("user32.dll", SetLastError = true)]
static extern int ExitWindowsEx(uint uFlags, uint dwReason);
उत्पादन कोड में आपको एपीआई कॉल के रिटर्न मानों की जांच करनी चाहिए, लेकिन मैंने उदाहरण को स्पष्ट करने के लिए इसे छोड़ दिया।
छोटा एवं सुन्दर। एक बाहरी कार्यक्रम को बुलाओ:
using System.Diagnostics;
void Shutdown()
{
Process.Start("shutdown.exe", "-s -t 00");
}
नोट: यह विंडोज के शटडाउन। Exe प्रोग्राम को कॉल करता है, इसलिए यह केवल तभी काम करेगा जब यह प्रोग्राम उपलब्ध हो। आपको विंडोज 2000 (जहां शटडाउन। केवल संसाधन किट में उपलब्ध है) या एक्सपी एंबेडेड पर समस्या हो सकती है ।
System.Diagnostics.Process.Start("shutdown", "/s /t 0")
कार्य करना चाहिए।
पुनरारंभ के लिए, यह / आर है
यह सीधे और साफ-सुथरे तरीके से, किसी भी डायलॉग के साथ पीसी बॉक्स को रीस्टार्ट करेगा।
shutdown /s /t 0 //For shutdown
shutdown /r /t 0 //For restart
shutdown /h /t 0 //For hibernate
इसके अलावा, उन्हें उसी परिणाम के लिए सीएमडी में टाइप करने का प्रयास करें।
ध्यान दें कि shutdown.exe
बस एक रैपर है InitiateSystemShutdownEx
, जो अंदर गायब होने वाली कुछ बारीकियों को प्रदान करता हैExitWindowsEx
मुझे ऊपर स्वीकार किए गए WMI विधि का उपयोग करने की कोशिश करने में परेशानी हुई क्योंकि मुझे हमेशा एक व्यवस्थापक के रूप में कार्यक्रम चलाने के बावजूद निजीकरण नहीं मिला।
समाधान स्वयं के लिए विशेषाधिकार का अनुरोध करने की प्रक्रिया के लिए था। मुझे इसका जवाब मिला http://www.dotnet247.com/247reference/msgs/58/292150.aspx पर रिचर्ड हिल नाम के एक व्यक्ति ने लिखा है।
मैंने अपने समाधान के मूल उपयोग को उस स्थिति में नीचे चिपका दिया है जो लिंक पुराना हो गया है।
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Runtime.InteropServices;
using System.Security;
using System.Diagnostics;
namespace PowerControl
{
public class PowerControl_Main
{
public void Shutdown()
{
ManagementBaseObject mboShutdown = null;
ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
mcWin32.Get();
if (!TokenAdjuster.EnablePrivilege("SeShutdownPrivilege", true))
{
Console.WriteLine("Could not enable SeShutdownPrivilege");
}
else
{
Console.WriteLine("Enabled SeShutdownPrivilege");
}
// 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())
{
try
{
mboShutdown = manObj.InvokeMethod("Win32Shutdown",
mboShutdownParams, null);
}
catch (ManagementException mex)
{
Console.WriteLine(mex.ToString());
Console.ReadKey();
}
}
}
}
public sealed class TokenAdjuster
{
// PInvoke stuff required to set/enable security privileges
[DllImport("advapi32", SetLastError = true),
SuppressUnmanagedCodeSecurityAttribute]
static extern int OpenProcessToken(
System.IntPtr ProcessHandle, // handle to process
int DesiredAccess, // desired access to process
ref IntPtr TokenHandle // handle to open access token
);
[DllImport("kernel32", SetLastError = true),
SuppressUnmanagedCodeSecurityAttribute]
static extern bool CloseHandle(IntPtr handle);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int AdjustTokenPrivileges(
IntPtr TokenHandle,
int DisableAllPrivileges,
IntPtr NewState,
int BufferLength,
IntPtr PreviousState,
ref int ReturnLength);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool LookupPrivilegeValue(
string lpSystemName,
string lpName,
ref LUID lpLuid);
[StructLayout(LayoutKind.Sequential)]
internal struct LUID
{
internal int LowPart;
internal int HighPart;
}
[StructLayout(LayoutKind.Sequential)]
struct LUID_AND_ATTRIBUTES
{
LUID Luid;
int Attributes;
}
[StructLayout(LayoutKind.Sequential)]
struct _PRIVILEGE_SET
{
int PrivilegeCount;
int Control;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] // ANYSIZE_ARRAY = 1
LUID_AND_ATTRIBUTES[] Privileges;
}
[StructLayout(LayoutKind.Sequential)]
internal struct TOKEN_PRIVILEGES
{
internal int PrivilegeCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
internal int[] Privileges;
}
const int SE_PRIVILEGE_ENABLED = 0x00000002;
const int TOKEN_ADJUST_PRIVILEGES = 0X00000020;
const int TOKEN_QUERY = 0X00000008;
const int TOKEN_ALL_ACCESS = 0X001f01ff;
const int PROCESS_QUERY_INFORMATION = 0X00000400;
public static bool EnablePrivilege(string lpszPrivilege, bool
bEnablePrivilege)
{
bool retval = false;
int ltkpOld = 0;
IntPtr hToken = IntPtr.Zero;
TOKEN_PRIVILEGES tkp = new TOKEN_PRIVILEGES();
tkp.Privileges = new int[3];
TOKEN_PRIVILEGES tkpOld = new TOKEN_PRIVILEGES();
tkpOld.Privileges = new int[3];
LUID tLUID = new LUID();
tkp.PrivilegeCount = 1;
if (bEnablePrivilege)
tkp.Privileges[2] = SE_PRIVILEGE_ENABLED;
else
tkp.Privileges[2] = 0;
if (LookupPrivilegeValue(null, lpszPrivilege, ref tLUID))
{
Process proc = Process.GetCurrentProcess();
if (proc.Handle != IntPtr.Zero)
{
if (OpenProcessToken(proc.Handle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
ref hToken) != 0)
{
tkp.PrivilegeCount = 1;
tkp.Privileges[2] = SE_PRIVILEGE_ENABLED;
tkp.Privileges[1] = tLUID.HighPart;
tkp.Privileges[0] = tLUID.LowPart;
const int bufLength = 256;
IntPtr tu = Marshal.AllocHGlobal(bufLength);
Marshal.StructureToPtr(tkp, tu, true);
if (AdjustTokenPrivileges(hToken, 0, tu, bufLength, IntPtr.Zero, ref ltkpOld) != 0)
{
// successful AdjustTokenPrivileges doesn't mean privilege could be changed
if (Marshal.GetLastWin32Error() == 0)
{
retval = true; // Token changed
}
}
TOKEN_PRIVILEGES tokp = (TOKEN_PRIVILEGES)Marshal.PtrToStructure(tu,
typeof(TOKEN_PRIVILEGES));
Marshal.FreeHGlobal(tu);
}
}
}
if (hToken != IntPtr.Zero)
{
CloseHandle(hToken);
}
return retval;
}
}
}
मैंने विंडोज 2003 सर्वर को बंद करने के लिए रूमारो की डब्ल्यूएमआई विधि की कोशिश की , लेकिन यह तब तक काम नहीं करेगा जब तक कि मैंने '[STAThread]' (यानी " सिंगल थ्रेडेड अपार्टमेंट " थ्रेडिंग मॉडल) को मुख्य () घोषणा में नहीं जोड़ा :
[STAThread]
public static void Main(string[] args) {
Shutdown();
}
मैंने तब एक थ्रेड से शटडाउन करने की कोशिश की, और उस काम को करने के लिए मुझे थ्रेड के "अपार्टमेंट स्टेट" को STA में भी सेट करना पड़ा:
using System.Management;
using System.Threading;
public static class Program {
[STAThread]
public static void Main(string[] args) {
Thread t = new Thread(new ThreadStart(Program.Shutdown));
t.SetApartmentState(ApartmentState.STA);
t.Start();
...
}
public static void Shutdown() {
// roomaroo's code
}
}
मैं सी # नोब हूं, इसलिए मैं सिस्टम को बंद करने के संदर्भ में पूरी तरह से एसटीए थ्रेड्स के महत्व के बारे में सुनिश्चित नहीं हूं (यहां तक कि मेरे द्वारा ऊपर पोस्ट किए गए लिंक को पढ़ने के बाद भी)। शायद कोई और विस्तृत कर सकता है ...?
Main()
नहीं है , तो इसकी आवश्यकता नहीं है [STAThread]
।
** विस्तृत उत्तर ...
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;
using System.Diagnostics;
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);
}
}
}
}
शटडाउन का उपयोग करें। पासिंग आर्ग्स के साथ समस्या से बचने के लिए, जटिल निष्पादन, WindowForms से निष्पादन PowerShell निष्पादित स्क्रिप्ट का उपयोग करें:
using System.Management.Automation;
...
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript("shutdown -a; shutdown -r -t 100;");
// invoke execution on the pipeline (collecting output)
Collection<PSObject> PSOutput = PowerShellInstance.Invoke();
}
System.Management.Automation.dll OS पर स्थापित होना चाहिए और GAC में उपलब्ध होना चाहिए।
मेरी अंग्रेजी के लिए खेद है।
कंप्यूटर को बंद करने के लिए कोई नेट नेट विधि नहीं है। आपको ExitWindows या ExitWindowsEx API कॉल को P / Invoke करने की आवश्यकता है।
अगर आप कंप्यूटर को दूर से बंद करना चाहते हैं तो आप उपयोग कर सकते हैं
Using System.Diagnostics;
किसी भी बटन पर क्लिक करें
{
Process.Start("Shutdown","-i");
}