• 整理笔记,发现判断是否联网的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);
            }
        }
    }

  • 相关阅读:
    ubuntu安装gnome桌面,ubuntu系统16升级到18
    重置root密码
    shell中tr的用法
    ubuntu14.04安装zabbix
    TTTTTTTTTTTTTT poj 1127 Jack Straws 线段相交+并查集
    TTTTTTTTTTTTT poj 3057 Evacuation 二分图匹配+bfs
    hdu 1208 Ignatius and the Princess III 划分数,dp
    Poj 3057 未AC http://poj.org/showsource?solution_id=15175171
    poj 3662 Telephone Lines dijkstra+二分搜索
    poj 3684 Physics Experiment 弹性碰撞
  • 原文地址:https://www.cnblogs.com/qq419524837/p/1455323.html
Copyright © 2020-2023  润新知