Tech Off Thread

5 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Detecting a Network Connection In Code

Back to Forum: Tech Off
  • User profile image
    Frankie Fresh

    I have the following code snippet that works fine under XP, but causes an exception in Windows 2000.


    "System.Management.ManagementException: Invalid query"


    What's the best way to get this to work under both OS's?




    try

    {

    ObjectQuery objectQuery = new

    ObjectQuery(

    "select * from Win32_NetworkAdapter where NetConnectionStatus=2"

    );

    ManagementObjectSearcher searcher =

    new ManagementObjectSearcher(objectQuery);

    int connectedNetworks = searcher.Get().Count;

    if (connectedNetworks > 0)

    {

    return true;

    }

    else

    {

    return false;

    }

    }

    catch(Exception ex)

    {

    // Windows 2000 machines will throw an exception

    return false;

     

    }

  • User profile image
    brussell

    I'm not exactly sure. I have code that works in a similar way to that and I just tried it on a Win2000 machine with .NET Framework on it and it worked.

    I have a program that gives you a list of all the options available in the Management stuff. It listed off my network items just fine.

    I have to go answer a phone and then can maybe help a bit more.

  • User profile image
    brussell

    I read somewhere that a WMI SDK is required to use those calls...
    I don't remember ever installing that specifically, but think it may work on a computer with the .NET Framework or something on it....


  • User profile image
    z33driver

    There is a Win32s API call you can use Checks out the default Connection Detection Strategy class inn the Smart Client offline App. Block. Look up Win Inet on msdn.

  • User profile image
    Karim

    Seems like all the WMI calls I want to use are XP/2003 only...

    Have you tried retrieving a web page using XMLHTTP?  That should even work on Windows 98.. LOL Ok it is about as efficient as using a sledgehammer to chop wood, but... it would WORK... LOL

    sorry... i am working off Flexoril, Percoset, ibuprofen and I'm not sure the 3 AM shot of Dilaudid has worn off... I should only make coding suggestions when I'm sober...  LOL

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.