Udostępnij za pośrednictwem


Detecting Offline State

So for my VSTO project, I need to be able to detect if I am in an online state. This detection will be pivotal in deciding if I should or can retrieve new data from my data providers (these include SharePoint and some other applications). I wanted to be able to key off of Internet Explorer's "Work Offline" feature so that I could easily test my offline state. I wrapped this functionality in a class called ConnectionManager. It really is just a wrapper around the Win32 call to InternetGetConnectedState. This checks to see if there is an internet connection defined and additionally if it is online or not. Here is the class:

Public Class ConnectionManager

   Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpdwFlags As Int32, ByVal dwReserved As Int32) As Boolean

    Private Const CONNECT_LAN As Long = &H2
    Private Const CONNECT_MODEM As Long = &H1
    Private Const CONNECT_PROXY As Long = &H4
    Private Const CONNECT_OFFLINE As Long = &H20
    Private Const CONNECT_CONFIGURED As Long = &H40
    Private Const CONNECT_RAS As Long = &H10

    Public Shared Function IsWebConnected(Optional ByRef ConnType As String = "") As Boolean
        Dim dwflags As Long
        Dim WebTest As Boolean
        WebTest = InternetGetConnectedState(dwflags, 0&)
        Select Case WebTest
            Case dwflags And CONNECT_LAN : ConnType = "LAN"
            Case dwflags And CONNECT_MODEM : ConnType = "Modem"
            Case dwflags And CONNECT_PROXY : ConnType = "Proxy"
            Case dwflags And CONNECT_OFFLINE : ConnType = "Offline"
            Case dwflags And CONNECT_CONFIGURED : ConnType = "Configured"
            Case dwflags And CONNECT_RAS : ConnType = "Remote"
        End Select
        Return WebTest
    End Function
End Class

So in my startup routine in my spreadsheet I can test to see the connectivity state:
            'Are we connected and online
            Dim connType As String = String.Empty
            Dim isOnline As Boolean = ConnectionManager.IsWebConnected(connType)
            isOnline = isOnline And connType <> "Offline"
            'Load data
            If (isOnline) Then

Notice that I have to check both that there is a connection and that the return type was not Offline. This seems to be needed since even an offline IE still returns true from the interop call.

Coming Soon:
My next writeup will have my task pane structure and a screen shot of what I'm actually building!