是否断网
2011-03-01 16:12
232 查看
记得大一的时候,一个校园网的几多同学朋友用i,每当自己开着在下载视频教程,然后就去左上课,点知道返来就发现,网又比人地用左,白白浪费滴电
后来就想到用C#做一个判断当前机子是否联网,假如无个话设置关机,米就可以悭翻滴电,环保撒。呵呵
namespace MIS
{
public partial class Close2 : Form
{
System.Net.NetworkInformation.Ping ping;
System.Net.NetworkInformation.PingReply res;
public Close2()
{
InitializeComponent();
}
private void Close2_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
ping = new System.Net.NetworkInformation.Ping();
try
{
res = ping.Send("http://www.google.cn/"); //
if (res.Status != System.Net.NetworkInformation.IPStatus.Success)
{
//没有联上Internet哦,做下自己想做个事情咯
timer1.Stop();
MIS.Close.DoExitWin(); //关机咯
}
}
catch(Exception er)
{
//里你,仲系当你断网咯,做下自己想做个事情咯
timer1.Stop();
MIS.Close.DoExitWin(); //关机咯
}
}
}
}
//关机程式
using System.Runtime.InteropServices; //
using Microsoft.Win32; //
namespace MIS
{
public class Close
{
/// <summary>
///关闭计算机
/// </summary>
[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;
public static void DoExitWin()
{
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(EWX_SHUTDOWN, 0);
}
}
}
后来就想到用C#做一个判断当前机子是否联网,假如无个话设置关机,米就可以悭翻滴电,环保撒。呵呵
namespace MIS
{
public partial class Close2 : Form
{
System.Net.NetworkInformation.Ping ping;
System.Net.NetworkInformation.PingReply res;
public Close2()
{
InitializeComponent();
}
private void Close2_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
ping = new System.Net.NetworkInformation.Ping();
try
{
res = ping.Send("http://www.google.cn/"); //
if (res.Status != System.Net.NetworkInformation.IPStatus.Success)
{
//没有联上Internet哦,做下自己想做个事情咯
timer1.Stop();
MIS.Close.DoExitWin(); //关机咯
}
}
catch(Exception er)
{
//里你,仲系当你断网咯,做下自己想做个事情咯
timer1.Stop();
MIS.Close.DoExitWin(); //关机咯
}
}
}
}
//关机程式
using System.Runtime.InteropServices; //
using Microsoft.Win32; //
namespace MIS
{
public class Close
{
/// <summary>
///关闭计算机
/// </summary>
[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;
public static void DoExitWin()
{
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(EWX_SHUTDOWN, 0);
}
}
}
相关文章推荐
- GridView删除时提示 是否删除
- 判断一个类型是否为可空类型(System.Nullable)
- 后台还原checkboxlit的是否被选中的属性
- FCKeditor的js验证以及FCKeditor内容是否为空判断
- go语言 浮点数比较是否相等
- python中set集合如何决定是否重复?
- java——多线程——单例模式的static方法和非static方法是否是线程安全的?
- a 标签中加 onclick事件,根据事件中的校验情况来决定是否执行a标签的链接
- delete未提交rollback是否会改变行号
- java 判断是否中文字符
- 离散题目3(判断是否为子集)
- 已知两条线段端点,判断是否相交及交点
- 转一讨论帖:如何测试一个门户网站是否可以支持10万用户同时在线?
- 判断一组单选按钮是否有被选中的
- 1.判断字符串中的字符是否Unique
- JAVA版判断两条线是否相交
- js 判断checkbox是否选中
- jQuery判断元素是否显示与隐藏
- 使用CMakeLists.txt 判断编译器是否支持C++11
- 百度地图API 判断点是否在圆形内