How do you determine if an Internet connection is available for your WinForms App?

前端 未结 8 874
生来不讨喜
生来不讨喜 2020-12-24 15:33

What is the best way to determine whether there is an available Internet connection for a WinForms app. (Programatically of course) I want to disable/hide certain functions

8条回答
  •  一个人的身影
    2020-12-24 16:08

    sbeskur's response has a bug in the translation of InternetGetConnectedState. The parameters are both DWORD's (first one is an LPDWORD). Both of these translate to int's in C# (technically, uint but int will work for most scenarios).

    Correct translation below.

    
    [DllImport("wininet.dll", SetLastError=true)] 
    public static extern bool InternetGetConnectedState(out int flags,int reserved);
    

提交回复
热议问题