整理笔记,发现判断是否联网的C#代码 --..--

 

记得大一的时候,一个校园网的几多同学朋友用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);
        }
    }
}

posted @ 2009-05-13 02:31  阿K&amp;LiveCai  阅读(4243)  评论(7编辑  收藏  举报