gpt4 book ai didi

c# - 如何在 C# 中查询互联网连接状态?

转载 作者:行者123 更新时间:2023-11-30 19:21:14 25 4
gpt4 key购买 nike

查找用户是否连接到 Internet 的最佳和快速方法是什么?

最佳答案

试试这个:

   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# - 如何在 C# 中查询互联网连接状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3802171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com