Enable/Disable network card

Hi.

Is there any possibility to control the network card ( enable/disable) using c# in .NET Framework 1.1

Thanks.Mircea


Answer this question

Enable/Disable network card

  • DMeglio

    yes it is possible to disable a particular adapter.
    But what would this buy you What kind of test results are you expecting


  • Irro

    Thank you very much.

    Mircea

  • Val P

    You must use win32 api.I think you can not do this with managed code.(But maybe there can be something in System.Management.)


    Here you must stop the device server.For this you must use ControlService in advapi32 .
    please look here : http://www.pinvoke.net/default.aspx/advapi32.ControlService

    Maybe this will give you an idea...

  • MichaelEber

    can you show the anser for all of us ,thanks you 


  • hlee

    Although I have never done this in code, I beleive this can be done using WMI.  The following links should provide the information you need:

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/wmisdk/wmi/wmi_tasks__networking.asp

    http://www.codeproject.com/csharp/wmimadeeasy.asp



  • sahina

    Can you point me to the right direction so I can solve this problem

    Thanks.
    Mircea

  • marina B.

    I want to simulate this, that's all.
    So, I am interesting in doing this and I wanna know the way how to do this.


  • YoungEngineer

    NO

  • SysDev

    Here is some sample C++ code that disables/enables all connections on the machine, but you should be able to tweak it to do what you need.  Note that this code has not been thoroughly tested and is just intended to get you started.

    If you search the internet for INetConnectionManager, you should be able to find some samples where these objects are used in C#.



    HRESULT DisableEnableConnections(BOOL bEnable)
    {
        HRESULT hr = E_FAIL;

        CoInitialize(NULL);

        INetConnectionManager *pNetConnectionManager = NULL;
     hr = CoCreateInstance(CLSID_ConnectionManager,
                              NULL,
                              CLSCTX_LOCAL_SERVER | CLSCTX_NO_CODE_DOWNLOAD,
                              IID_INetConnectionManager,
                              reinterpret_cast<LPVOID *>(&pNetConnectionManager)
                             );
        if (SUCCEEDED(hr))
        {
            /*
      Get an enumurator for the set of connections on the system
      */
            IEnumNetConnection* pEnumNetConnection;
            pNetConnectionManager->EnumConnections(NCME_DEFAULT, &pEnumNetConnection);

            ULONG ulCount = 0;
            BOOL fFound = FALSE;

            hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
           
            HRESULT hrT = S_OK;

            /*  
       Enumurate through the list of adapters on the system and look for the one we want
       NOTE: To include per-user RAS connections in the list, you need to set the COM
       Proxy Blanket on all the interfaces. This is not needed for All-user RAS
       connections or LAN connections.
      */
            do
            {
                NETCON_PROPERTIES* pProps = NULL;
                INetConnection *   pConn;

                /*
       Find the next (or first connection)
       */
                hrT = pEnumNetConnection->Next(1, &pConn, &ulCount);

                if (SUCCEEDED(hrT) && 1 == ulCount)
                {
        /*
        Get the connection properties
        */
                    hrT = pConn->GetProperties(&pProps);

        if (S_OK == hrT)
                    {
         printf("* %S\n", pProps->pszwName);

         if (bEnable)
         {
          printf("      Enabling adapter: %S...\n",pProps->pszwName);
          hr = pConn->Connect();
         }
         else
         {
          printf("      Disabling adapter: %S...\n",pProps->pszwName);
          hr = pConn->Disconnect();
         }
                       

                        CoTaskMemFree (pProps->pszwName);
                        CoTaskMemFree (pProps->pszwDeviceName);
                        CoTaskMemFree (pProps);
                    }

                    pConn->Release();
                    pConn = NULL;
                }

            } while (SUCCEEDED(hrT) && 1 == ulCount && !fFound);

            if (FAILED(hrT))
            {
                hr = hrT;
            }

            pEnumNetConnection->Release();
        }
       
        if (FAILED(hr) && hr != HRESULT_FROM_WIN32(ERROR_RETRY))
        {
            printf("Could not enable or disable connection (0x%08x)\r\n", hr);
        }

        pNetConnectionManager->Release();

        CoUninitialize();

     return hr;
    }


     



  • Rob-Nick

    Thank you for your answer.
    I was using the Mike Flasko sugestion and now I am able to do what I wanted.
    Thank you to all of you.

    Best Regards.
    Mircea


  • Radovici

    I am not clear when you say enable/disable a specific connection.
    Why would you want to do that
    What is the scenario

  • -Terry_K-

    When you look in network connections from "Start->Settings" you see at least one connection. One of these is "Local Area Connection". I want to interrupt this one, not the others.
    I don't know if I was clear.
    The scenario is this: I want to interrupt this connection for test my client-server application; in fact, I want to create a test project that can be run without any user  interaction.
    Mircea

  • Rick Winter

    It is a good point.
    I figured out that I don't need to enable/disable the network card: I am interested in disabling/enabling a specific connection, not the whole connections from the network card.
    Any ideea
    Thanks.
    Mircea


  • Jason Hamlin

    I think the answer he is refering to is the one from Mike Flasko:

     Mike Flasko wrote:

    Although I have never done this in code, I beleive this can be done using WMI.  The following links should provide the information you need:

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/wmisdk/wmi/wmi_tasks__networking.asp

    http://www.codeproject.com/csharp/wmimadeeasy.asp



  • Enable/Disable network card