locked
Check if the local computer has internet connection. RRS feed

  • Question

  • is it possible in c# to check if an internet connetion exists in the local computer? can someone give me a code for that.

     

    thanks,regards,

     

    Monday, May 26, 2008 1:52 AM

Answers

  • Hello, here is a small code block I made that demonstrates the way to do it. 

     

    Code Snippet

    class Program

    {

    [Flags]

    enum ConnectionState : int {

    INTERNET_CONNECTION_MODEM = 0x1,

    INTERNET_CONNECTION_LAN = 0x2,

    INTERNET_CONNECTION_PROXY = 0x4,

    INTERNET_RAS_INSTALLED = 0x10,

    INTERNET_CONNECTION_OFFLINE = 0x20,

    INTERNET_CONNECTION_CONFIGURED = 0x40

    }

    // P/Invoke

    [DllImport("wininet.dll")]

    private extern static bool InternetGetConnectedState(ref ConnectionState lpdwFlags, int dwReserved);

    // Simplifies the Win32 API call to InternetGetConnectedState.

    public static bool IsConnected( )

    {

    ConnectionState flags = 0;

    InternetGetConnectedState(ref flags, 0);

    return (flags & ConnectionState.INTERNET_CONNECTION_MODEM) != 0;

    }

    static void Main(string[] args) {

    Console.WriteLine(IsConnected());

    Console.ReadKey();

    }

    }

     

     

    Regards.

     

    Monday, May 26, 2008 6:36 AM
  •  ml_coro wrote:

    These are the exceptions...

     

    Error 1 The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\mcoro\My Documents\Visual Studio 2008\Projects\ConsoleApplication11\ConsoleApplication11\Program.cs 34 10 ConsoleApplication11

    Error 2 The type or namespace name 'DllImportAttribute' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\mcoro\My Documents\Visual Studio 2008\Projects\ConsoleApplication11\ConsoleApplication11\Program.cs 34 10 ConsoleApplication11

     

    Did i miss another "Using " statement at the header?

    Add the following namespace.

     

    Code Snippet

    using System.Runtime.InteropServices;

     

     

    Regards.
    Monday, May 26, 2008 7:25 AM

All replies

  • Hello, here is a small code block I made that demonstrates the way to do it. 

     

    Code Snippet

    class Program

    {

    [Flags]

    enum ConnectionState : int {

    INTERNET_CONNECTION_MODEM = 0x1,

    INTERNET_CONNECTION_LAN = 0x2,

    INTERNET_CONNECTION_PROXY = 0x4,

    INTERNET_RAS_INSTALLED = 0x10,

    INTERNET_CONNECTION_OFFLINE = 0x20,

    INTERNET_CONNECTION_CONFIGURED = 0x40

    }

    // P/Invoke

    [DllImport("wininet.dll")]

    private extern static bool InternetGetConnectedState(ref ConnectionState lpdwFlags, int dwReserved);

    // Simplifies the Win32 API call to InternetGetConnectedState.

    public static bool IsConnected( )

    {

    ConnectionState flags = 0;

    InternetGetConnectedState(ref flags, 0);

    return (flags & ConnectionState.INTERNET_CONNECTION_MODEM) != 0;

    }

    static void Main(string[] args) {

    Console.WriteLine(IsConnected());

    Console.ReadKey();

    }

    }

     

     

    Regards.

     

    Monday, May 26, 2008 6:36 AM
  •  

    Ehmmm.. where can i find this wininet.dll? an exception was thrown because of it..
    Monday, May 26, 2008 6:43 AM
  • Monday, May 26, 2008 7:08 AM
  •  ml_coro wrote:

     

    Ehmmm.. where can i find this wininet.dll? an exception was thrown because of it..

    It should be fairly available on your system, look at the requirements section on the link below.

     

    Win32API: InternetGetConnectedState Function

    http://msdn.microsoft.com/en-us/library/aa384702(VS.85).aspx

     

    About WinINet

    http://msdn.microsoft.com/en-us/library/aa383630(VS.85).aspx

     

    P.s. Please post the exception your getting.

    Monday, May 26, 2008 7:15 AM
  • These are the exceptions...

     

    Error 1 The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\mcoro\My Documents\Visual Studio 2008\Projects\ConsoleApplication11\ConsoleApplication11\Program.cs 34 10 ConsoleApplication11

    Error 2 The type or namespace name 'DllImportAttribute' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\mcoro\My Documents\Visual Studio 2008\Projects\ConsoleApplication11\ConsoleApplication11\Program.cs 34 10 ConsoleApplication11

     

    Did i miss another "Using " statement at the header?

    Monday, May 26, 2008 7:20 AM
  •  ml_coro wrote:

    These are the exceptions...

     

    Error 1 The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\mcoro\My Documents\Visual Studio 2008\Projects\ConsoleApplication11\ConsoleApplication11\Program.cs 34 10 ConsoleApplication11

    Error 2 The type or namespace name 'DllImportAttribute' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\mcoro\My Documents\Visual Studio 2008\Projects\ConsoleApplication11\ConsoleApplication11\Program.cs 34 10 ConsoleApplication11

     

    Did i miss another "Using " statement at the header?

    Add the following namespace.

     

    Code Snippet

    using System.Runtime.InteropServices;

     

     

    Regards.
    Monday, May 26, 2008 7:25 AM
  •  

    It worked... yet it returned false even if my system is connected to the internet. i changed the code a bit ...

     

    thanks a lot dude.

     

    public static bool IsConnected()

    {

    ConnectionState flags = 0;

    InternetGetConnectedState(ref flags, 0);

    return (flags & ConnectionState.INTERNET_CONNECTION_CONFIGURED) == 0;

     

    }

    Monday, May 26, 2008 7:32 AM
  • The network and internet configuration vary so you might want to check for INTERNET_CONNECTION_MODEM too.

     

    Regards.

    Monday, May 26, 2008 7:46 AM
  • Hi,
            for this you can use both native and managed methods.
    Native method:
    Code Snippet

    [DllImport("wininet.dll")]
    private extern static bool InternetGetConnectedState( out int Description, int ReservedValue ) ;

    int iDesc;

    bool bRet = InternetGetConnectedState (iDesc, 0);


    Managed option:
    Code Snippet

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("www.google.com");
    HttpWebResponse response = (HttpWebResponse) request.GetResponse();
    bool bStatus;
    if (HttpStatusCode.OK == response.StatusCode)
    {
    response.Close();
    bStatus = true;
    }
    else
    {
    bStatus = false;
    }


    Regards,

    GANESAN MURUGESAN
    Monday, May 26, 2008 11:54 AM
  •  

    i tried to use the managed option but i got an exception. again, did i miss a using directive.?what is it?

     

    thanks

    Tuesday, May 27, 2008 12:31 AM