确定 WinForms 应用程序是否有可用的 Internet 连接的最佳方法是什么。 (当然是编程方式)如果用户未连接到 Internet,我想禁用/隐藏某些功能。
最佳答案
以下将确定您是否已连接到网络,但这并不一定意味着您已连接到 Internet:
NetworkInterface.GetIsNetworkAvailable()
这是 Steve 代码的 C# 翻译,看起来还不错:
private static int ERROR_SUCCESS = 0;
public static bool IsInternetConnected() {
long dwConnectionFlags = 0;
if (!InternetGetConnectedState(dwConnectionFlags, 0))
return false;
if(InternetAttemptConnect(0) != ERROR_SUCCESS)
return false;
return true;
}
[DllImport("wininet.dll", SetLastError=true)]
public static extern int InternetAttemptConnect(uint res);
[DllImport("wininet.dll", SetLastError=true)]
public static extern bool InternetGetConnectedState(out int flags,int reserved);
关于c# - 您如何确定 Internet 连接是否可用于您的 WinForms 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/194742/