如何用代码实现关闭休眠功能 如题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 请调用windows api 实现此功能用于关机的api 如下:ExitWindowsExitWindowsExInitiateSystemShutdown 给你一段我自己写的和你要的相关代码:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;using System.Security.Principal;namespace XP_Time_Closed{ public partial class Form1 : Form { String time = ""; [DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Auto)] private static extern int ExitWindowsEx(int uFlags, int dwReserved); [DllImport("kernel32.dll", CharSet = CharSet.Auto)] private static extern int GetCurrentProcess(); [DllImport("kernel32.dll", CharSet = CharSet.Auto)] private static extern int GetLastError(); [DllImport("advapi32", CharSet = CharSet.Auto)] private static extern int OpenProcessToken(int ProcessHandle, uint DesiredAccess, ref int TokenHandle); [DllImport("advapi32", CharSet = CharSet.Auto)] private static extern int LookupPrivilegeValue(String lpSystemName, String lpName, ref LUID lpLuid); [DllImport("advapi32", CharSet = CharSet.Auto)] private static extern int AdjustTokenPrivileges(int TokenHandle, bool DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, int BufferLength, int PreviousState, int ReturnLength); [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct LUID { public uint LowPart; public uint HighPart; }; [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct TOKEN_PRIVILEGES { public uint PrivilegeCount; public LUID Luid; public uint Attributes; }; const uint TOKEN_ADJUST_PRIVILEGES = 0x20; const uint TOKEN_QUERY = 0x8; const uint SE_PRIVILEGE_ENABLED = 0x2; LUID tmpLuid = new LUID(); TOKEN_PRIVILEGES tkp = new TOKEN_PRIVILEGES(); //TOKEN_PRIVILEGES tkpNewButIgnored = new TOKEN_PRIVILEGES(); int lBufferNeeded = 0; public Form1() { InitializeComponent(); } private void btn_Ok_Click(object sender, EventArgs e) { try { if (this.cmb_hour.SelectedItem.ToString() == null && this.cmb_min.SelectedItem.ToString() == null&&cmb_sec.SelectedItem.ToString()==null) { MessageBox.Show("请选择关机的时间","Error",MessageBoxButtons.OK,MessageBoxIcon.Warning); } else { int hour = this.cmb_hour.SelectedIndex; int min = this.cmb_min.SelectedIndex; int sec = this.cmb_sec.SelectedIndex; time=dateTimePicker1.Value.ToLongDateString()+cmb_hour.SelectedItem.ToString()+":"+cmb_min.SelectedItem.ToString()+":"+cmb_sec.SelectedItem.ToString(); timer1.Start(); } MessageBox.Show("系统将在"+dateTimePicker1.Value.ToLongDateString()+cmb_hour.SelectedItem.ToString()+"时"+cmb_min.SelectedItem.ToString()+"分"+cmb_sec.SelectedItem.ToString()+"秒关机"); } catch (Exception err) { MessageBox.Show("未选定时间无法关机!"+err.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Warning); } } private void timer1_Tick(object sender, EventArgs e) { String nowtime =System.DateTime.Now.ToLongDateString()+System.DateTime.Now.ToLongTimeString(); if (nowtime.Equals(time))//重要代码片断!!!!!!!!!!!!!!!!!!!!!!!!!! { int hdlTokenHandle = 0; int hdlProcessHandle = GetCurrentProcess(); OpenProcessToken(hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY), ref hdlTokenHandle); LookupPrivilegeValue("", "SeShutdownPrivilege", ref tmpLuid); tkp.PrivilegeCount = 1; tkp.Luid = tmpLuid; tkp.Attributes = SE_PRIVILEGE_ENABLED; AdjustTokenPrivileges(hdlTokenHandle, false, ref tkp, Marshal.SizeOf(tkp), 0, lBufferNeeded); if (GetLastError() == 0) { ExitWindowsEx((int)EWX.EWX_SHUTDOWN | (int)EWX.EWX_FORCE, 0); } } } public enum EWX : int { EWX_FORCE = 4,//强迫终止没有响应进程 EWX_LOGOFF = 0,//终止进程,然后注销 EWX_REBOOT = 2,//重新引导系统 EWX_SHUTDOWN = 1//关闭系统 } private void Form1_Load(object sender, EventArgs e) { skinEngine1.SkinFile = "WaveColor1.ssk"; timer2.Start(); this.notifyIcon1.Text = "系统定时关机系统"+"\n"; } private void btn_Can_Click(object sender, EventArgs e) { timer1.Stop(); MessageBox.Show("关机时间已经被取消!","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning); } private void timer2_Tick(object sender, EventArgs e) { lbl_curTime.Text = System.DateTime.Now.ToLongDateString() + System.DateTime.Now.ToLongTimeString(); } private void 显示窗口ToolStripMenuItem_Click(object sender, EventArgs e) { this.Show(); } private void 隐藏窗口ToolStripMenuItem_Click(object sender, EventArgs e) { this.Hide(); } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } }}望对你有帮助和启示作用! http://www.cnblogs.com/wangsaokui/articles/10015.aspx 我想问问如何获取Oracle数据库的一些基本信息 那个简单正则 关闭数据库连接的一些疑惑 怎样将一个文件夹下的所有文件的名字导入到数据库中 如何实现oracle一次更新多行记录 c#绑定下拉列表问题!!! 请各位推荐一下asp.net c#的好书 如何读取DATASET 中N行M列的数据?谢谢! 用谷歌浏览器获取JSON数据地址,如何使用,谢谢 如何得到当前的时间? 【100分】这样的功不知能不能实现 如何通过Assembly的实例生成一个exe或dll?
ExitWindows
ExitWindowsExInitiateSystemShutdown
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Security.Principal;namespace XP_Time_Closed
{
public partial class Form1 : Form
{
String time = "";
[DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Auto)]
private static extern int ExitWindowsEx(int uFlags, int dwReserved);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern int GetCurrentProcess();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern int GetLastError();
[DllImport("advapi32", CharSet = CharSet.Auto)]
private static extern int OpenProcessToken(int ProcessHandle, uint DesiredAccess, ref int TokenHandle);
[DllImport("advapi32", CharSet = CharSet.Auto)]
private static extern int LookupPrivilegeValue(String lpSystemName, String lpName, ref LUID lpLuid);
[DllImport("advapi32", CharSet = CharSet.Auto)]
private static extern int AdjustTokenPrivileges(int TokenHandle, bool DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, int BufferLength, int PreviousState, int ReturnLength);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct LUID
{
public uint LowPart;
public uint HighPart;
}; [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct TOKEN_PRIVILEGES
{
public uint PrivilegeCount;
public LUID Luid;
public uint Attributes;
};
const uint TOKEN_ADJUST_PRIVILEGES = 0x20;
const uint TOKEN_QUERY = 0x8;
const uint SE_PRIVILEGE_ENABLED = 0x2;
LUID tmpLuid = new LUID();
TOKEN_PRIVILEGES tkp = new TOKEN_PRIVILEGES();
//TOKEN_PRIVILEGES tkpNewButIgnored = new TOKEN_PRIVILEGES(); int lBufferNeeded = 0; public Form1()
{
InitializeComponent();
} private void btn_Ok_Click(object sender, EventArgs e)
{
try
{
if (this.cmb_hour.SelectedItem.ToString() == null && this.cmb_min.SelectedItem.ToString() == null&&cmb_sec.SelectedItem.ToString()==null)
{
MessageBox.Show("请选择关机的时间","Error",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
else
{
int hour = this.cmb_hour.SelectedIndex;
int min = this.cmb_min.SelectedIndex;
int sec = this.cmb_sec.SelectedIndex;
time=dateTimePicker1.Value.ToLongDateString()+cmb_hour.SelectedItem.ToString()+":"+cmb_min.SelectedItem.ToString()+":"+cmb_sec.SelectedItem.ToString();
timer1.Start();
}
MessageBox.Show("系统将在"+dateTimePicker1.Value.ToLongDateString()+cmb_hour.SelectedItem.ToString()+"时"+cmb_min.SelectedItem.ToString()+"分"+cmb_sec.SelectedItem.ToString()+"秒关机");
}
catch (Exception err)
{
MessageBox.Show("未选定时间无法关机!"+err.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
} private void timer1_Tick(object sender, EventArgs e)
{
String nowtime =System.DateTime.Now.ToLongDateString()+System.DateTime.Now.ToLongTimeString();
if (nowtime.Equals(time))//重要代码片断!!!!!!!!!!!!!!!!!!!!!!!!!!
{
int hdlTokenHandle = 0;
int hdlProcessHandle = GetCurrentProcess();
OpenProcessToken(hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY), ref hdlTokenHandle);
LookupPrivilegeValue("", "SeShutdownPrivilege", ref tmpLuid);
tkp.PrivilegeCount = 1;
tkp.Luid = tmpLuid;
tkp.Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hdlTokenHandle, false, ref tkp, Marshal.SizeOf(tkp), 0, lBufferNeeded);
if (GetLastError() == 0)
{
ExitWindowsEx((int)EWX.EWX_SHUTDOWN | (int)EWX.EWX_FORCE, 0);
}
}
} public enum EWX : int
{
EWX_FORCE = 4,//强迫终止没有响应进程
EWX_LOGOFF = 0,//终止进程,然后注销
EWX_REBOOT = 2,//重新引导系统
EWX_SHUTDOWN = 1//关闭系统
} private void Form1_Load(object sender, EventArgs e)
{
skinEngine1.SkinFile = "WaveColor1.ssk";
timer2.Start();
this.notifyIcon1.Text = "系统定时关机系统"+"\n";
} private void btn_Can_Click(object sender, EventArgs e)
{
timer1.Stop();
MessageBox.Show("关机时间已经被取消!","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
} private void timer2_Tick(object sender, EventArgs e)
{
lbl_curTime.Text = System.DateTime.Now.ToLongDateString() + System.DateTime.Now.ToLongTimeString();
} private void 显示窗口ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Show();
} private void 隐藏窗口ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
} private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}望对你有帮助和启示作用!