整理筆記,發現判斷是否連網的C#代碼 –..–

來源:互聯網
上載者:User

 

記得大一的時候,一個校園網的幾多同學朋友用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);
        }
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.