Sort notes and find C # code to determine whether the network is connected -..-

Source: Internet
Author: User

 

I remember when I was a freshman, I was used by many friends on a campus network. Whenever I was driving and downloading video tutorials, I went to class on the left and found out when I came back, the Internet is more than the land use left, waste of electricity

Later, I thought of using C # To determine whether the current machine is connected to the Internet. If there is no such thing as shutdown, the rice will be able to be flushed and the electricity will be flushed for environmental protection. Haha

Namespace MIS
{
Public partial class close2: Form
{
System. net. networkinformation. Ping;
System. net. networkinformation. pingreply res;

Public close2 ()
{
Initializecomponent ();
}

Private void close2_load (Object sender, eventargs E)
{
}

Private void timereffectick (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)
{
// No Internet connection. Do what you want to do.
Timer1.stop ();

Mis. Close. doexitwin (); // shut down
}
}
Catch (exception ER)
{
// You, Zhong shu. When you break the network, do what you want to do.
Timer1.stop ();

Mis. Close. doexitwin (); // shut down
}
}

}
}

 

// Shut down the program
Using system. runtime. interopservices ;//
Using Microsoft. Win32 ;//

Namespace MIS
{
Public class close
{
/// <Summary>
/// Close the computer
/// </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;

Integer 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 );
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.