如何查询在C#中的网络连接状态? [英] How to query internet connection status in C# ?
本文介绍了如何查询在C#中的网络连接状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是最好的,并找到用户连接到互联网或没有快捷方式
what is best and Quick way to find user connected to internet or not ?
推荐答案
试试这个:
1: /// <summary>
2: /// Performs actions on the network
3: /// </summary>
4: public sealed class NetworkHandler
5: {
6: /// <summary>
7: /// Private constructor to prevent compiler from generating one
8: /// since this class only holds static methods and properties
9: /// </summary>
10: NetworkHandler() { }
11:
12: /// <summary>
13: /// SafeNativeMethods Class that holds save native methods
14: /// while suppressing unmanaged code security
15: /// </summary>
16: [SuppressUnmanagedCodeSecurityAttribute]
17: internal static class SafeNativeMethods
18: {
19: // Extern Library
20: // UnManaged code - be careful.
21: [DllImport("wininet.dll", CharSet = CharSet.Auto)]
22: [return: MarshalAs(UnmanagedType.Bool)]
23: private extern static bool
24: InternetGetConnectedState(out int Description, int ReservedValue);
25:
26: /// <summary>
27: /// Determines if there is an active connection on this computer
28: /// </summary>
29: /// <returns></returns>
30: public static bool HasActiveConnection()
31: {
32: int desc;
33: return InternetGetConnectedState(out desc, 0);
34: }
35: }
36: }
来源:的http:/ /blog.dotnetclr.com/archive/2007/09/24/Check-for-internet-connection---Method-2.aspx
这篇关于如何查询在C#中的网络连接状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文