当前位置:编程学习 > C#/ASP.NET >>

C#关机注销==问题

用C#怎么实现关机.注销 重启 等操作啊 --------------------编程问答-------------------- shutdown -a -i -w -s -t .. --------------------编程问答-------------------- 调用API函数--注销计算机.
加入如下函数.还有加入命名空间.SyStem.Runtime.InteropServices;
[DllImport("user32.dll",EntryPoint="ExitWindowsEx",CharSet=CharSet.Ansi]
private static extern in ExitWindowsEx(int uFlags,int dwReserved);

ExitWindowsEx(0,0);//注销.把它放在你想放在的事件中.即可完成功能.

//关机.
System.Diagnostics.Process myProcess=new System.Diagnostics.Process();
myProcess.StartInfo.FileName="cmd.exe";
myProcess.StartInfo.UseShellExecute=false;
myProcess.StartInfo.RedirectStandardInput=true;
myProcess.StartInfo.RedirectStandardError=true;
myProcess.StartInfo.CreatNoWindow=true;
myProcess.Start();//
myProcess.StandardInput.WriteLine("shutdown -s -t 0");

//重启一样:
System.Diagnostics.Process myProcess=new System.Diagnostics.Process();
myProcess.StartInfo.FileName="cmd.exe";
myProcess.StartInfo.UseShellExecute=false;
myProcess.StartInfo.RedirectStandardInput=true;
myProcess.StartInfo.RedirectStandardError=true;
myProcess.StartInfo.CreatNoWindow=true;
myProcess.Start();//
myProcess.StandardInput.WriteLine("shutdown -r -t 0");

//这样可以让所以dos命令都能实现.经典吧.我个人认为.呵呵, --------------------编程问答-------------------- using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace 文件和系统操作
{

public class 注销和关闭计算机 : System.Windows.Forms.Form
{
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct TokPrivlLuid
{
public int Count;
public long Luid;
public int Attr;
}
// GetCurrentProcess函数返回当前进程的一个句柄
[DllImport("kernel32.dll",ExactSpelling=true)]
public static extern IntPtr GetCurrentProcess();
// OpenProcessToken 函数打开一个进程的访问代号
[DllImport("advapi32.dll",ExactSpelling=true,SetLastError=true)]
public static extern bool OpenProcessToken(IntPtr ProcessHandles, int DesiredAccess, ref IntPtr TokenHandle);
// LookupPrivilegeValue 函数获得本地唯一标识符(LUID),用于在特定系统中表示特定优先权
[DllImport("advapi32.dll",SetLastError=true)]
public static extern bool  LookupPrivilegeValue(string lpSystemName, string lpName, ref long lpLuid);
// AdjustTokenPrivileges 函数使允许或者禁用指定访问记号的优先权
// 允许或者禁用优先权需要TOKEN_ADJUST_PRIVILEGES 访问权限
[DllImport("advapi32.dll",ExactSpelling=true,SetLastError=true)]
public static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, bool DisableAllPrivileges, 
ref TokPrivlLuid NewState, int BufferLength, IntPtr PreviousState, IntPtr ReturnLength);
// ExitWindowsEx 函数可以退出登陆、关机或者重新启动系统
[DllImport("user32.dll",ExactSpelling=true,SetLastError=true)]
public static extern bool ExitWindowsEx(int 易做图, int rea);

private System.Threading.Timer timer;
private const int SE_PRIVILEGE_ENABLED = 0x00000002;
private const int TOKEN_QUERY = 0x00000008;
private const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
private const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
private const int EWX_LOGOFF = 0x00000000;//注销
private const int EWX_SHUTDOWN = 0x00000001;//关机
private const int EWX_REBOOT = 0x00000002;//重起
private const int EWX_FORCE = 0x00000004;

private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;

private System.ComponentModel.Container components = null;

public 注销和关闭计算机()
{
InitializeComponent();

this.textBox1.Text = (Environment.TickCount / (1000 * 60)).ToString() + "分钟";
timer = new System.Threading.Timer(new TimerCallback(OnTimer), null, 0, 1000);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
// 
// label1
// 
this.label1.Location = new System.Drawing.Point(56, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(168, 23);
this.label1.TabIndex = 0;
this.label1.Text = "系统已运行时间";
// 
// textBox1
// 
this.textBox1.Location = new System.Drawing.Point(56, 72);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(176, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(40, 144);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(56, 23);
this.button1.TabIndex = 2;
this.button1.Text = "关闭";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// button2
// 
this.button2.Location = new System.Drawing.Point(128, 144);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(56, 23);
this.button2.TabIndex = 3;
this.button2.Text = "注销";
this.button2.Click += new System.EventHandler(this.button2_Click);
// 
// button3
// 
this.button3.Location = new System.Drawing.Point(216, 144);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(56, 23);
this.button3.TabIndex = 4;
this.button3.Text = "重起";
this.button3.Click += new System.EventHandler(this.button3_Click);
// 
// 注销和关闭计算机
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(304, 214);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "注销和关闭计算机";
this.Text = "注销和关闭计算机";
this.ResumeLayout(false);

}
#endregion

private static void RebootCommand(int 易做图)
{
bool ok;
TokPrivlLuid 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(易做图, 0);
}

//获得系统已运行的时间
private void OnTimer(object state)
{
this.textBox1.Text = (Environment.TickCount / (1000 * 60)).ToString() + "分钟";
this.textBox1.Refresh();
}

private void button1_Click(object sender, System.EventArgs e)
{
RebootCommand(EWX_SHUTDOWN + EWX_FORCE);
}

private void button2_Click(object sender, System.EventArgs e)
{
RebootCommand(EWX_LOGOFF + EWX_FORCE);
}

private void button3_Click(object sender, System.EventArgs e)
{
RebootCommand(EWX_REBOOT + EWX_FORCE);
}
}
}
--------------------编程问答-------------------- mark --------------------编程问答-------------------- mark --------------------编程问答-------------------- mark --------------------编程问答-------------------- 我顺便也用,学习一下 --------------------编程问答--------------------

using System.Runtime.InteropServices;

        #region 微软提供的关机接口 调用系统的 kernel32.dll  advapi32.dll user32.dll 实现的关机
        [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 DoFlag, 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 static bool DoExitWin(int DoFlag)
        {
            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(DoFlag, 0);
            return ok;
        }

        /**/
        /// <summary>
        /// 重新启动
        /// </summary>
        public static bool Reboot()
        {
            return DoExitWin(EWX_FORCE | EWX_REBOOT);
        }

        /**/
        /// <summary>
        /// 关机
        /// </summary>
        public static bool PowerOff()
        {
            return DoExitWin(EWX_FORCE | EWX_POWEROFF);
        }

        /**/
        /// <summary>
        /// 注销
        /// </summary>
        public static bool LogOff()
        {
            return DoExitWin(EWX_FORCE | EWX_LOGOFF);
        }
        #endregion
--------------------编程问答-------------------- 不错正是想要的。。。。。。。。 --------------------编程问答-------------------- using System.Runtime.InteropServices;
[Flags]
public enum ExitWindows : uint
{
LogOff = 0x00,      //注销
            ShutDown = 0x01,    //关机
            Reboot = 0x02,      //重启
            Force = 0x04,
PowerOff = 0x08,
ForceIfHung = 0x10
}
[Flags]
public enum ShutdownReason : uint
{
MajorApplication = 0x00040000,
MajorHardware = 0x00010000,
MajorLegacyApi = 0x00070000,
MajorOperatingSystem = 0x00020000,
MajorOther = 0x00000000,
MajorPower = 0x00060000,
MajorSoftware = 0x00030000,
MajorSystem = 0x00050000,
MinorBlueScreen = 0x0000000F,
MinorCordUnplugged = 0x0000000b,
MinorDisk = 0x00000007,
MinorEnvironment = 0x0000000c,
MinorHardwareDriver = 0x0000000d,
MinorHotfix = 0x00000011,
MinorHung = 0x00000005,
MinorInstallation = 0x00000002,
MinorMaintenance = 0x00000001,
MinorMMC = 0x00000019,
MinorNetworkConnectivity = 0x00000014,
MinorNetworkCard = 0x00000009,
MinorOther = 0x00000000,
MinorOtherDriver = 0x0000000e,
MinorPowerSupply = 0x0000000a,
MinorProcessor = 0x00000008,
MinorReconfig = 0x00000004,
MinorSecurity = 0x00000013,
MinorSecurityFix = 0x00000012,
MinorSecurityFixUninstall = 0x00000018,
MinorServicePack = 0x00000010,
MinorServicePackUninstall = 0x00000016,
MinorTermSrv = 0x00000020,
MinorUnstable = 0x00000006,
MinorUpgrade = 0x00000003,
MinorWMI = 0x00000015,
FlagUserDefined = 0x40000000,
FlagPlanned = 0x80000000
}
[DllImport("user32.dll")]
static extern bool ExitWindowsEx(ExitWindows uFlags, ShutdownReason dwReason);
[STAThread]
static void Main(string[] args)
{
ExitWindowsEx(ExitWindows.LogOff, ShutdownReason.MajorOther & ShutdownReason.MinorOther);
//这个语句将实现计算机注销操作   
        }


我正在使用《Csdn收音机》第一时间获取最新动态! --------------------编程问答-------------------- concern
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,