C# WMI: Throws an error when I try to enable/disable PPPoE adapter

时间:2016-05-11 11:34:46

标签: c# wmi

I'm trying to enable/disable my PPPoE adapter according to this answer.
It works great with normal adapters but not with PPPoE which throws an error saying :

An error occurred while querying for WMI data: Invalid method Parameter(s)

The adapter name is correct I used WMI Query tool for that purpose but I have no idea what params need to be set. Any help would be much appreciated.

Edit

Here's the code I used:

    static void Main(string[] args)
    {
        try
        {
            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher("root\\CIMV2",
                "SELECT * FROM Win32_NetworkAdapter WHERE Name = 'WAN Miniport (PPPOE)'");

            foreach (ManagementObject queryObj in searcher.Get())
            {
                queryObj.InvokeMethod("Enable", null);
                //Console.WriteLine("Name: {0}", queryObj["Name"]);
            }
        }
        catch (ManagementException e)
        {
            Console.WriteLine("An error occurred while querying for WMI data: " + e.Message);
        }
        Console.ReadKey();
    }

1 个答案:

答案 0 :(得分:0)

好的,我已经找到了DotRas的方法,这里是连接/断开PPPoE连接的代码(AKA拨号):

using System;
using System.Linq;
using System.Net;
using DotRas;

namespace Test_Reconnect_PPPoE
{
    class Program
    {
        public static void Main(string[] args)
        {
            // Connect
            using (RasDialer dialer = new RasDialer())
            {
                dialer.EntryName = "Your Entry (Connection Name)";
                dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User);
                dialer.Credentials = new NetworkCredential("username", "password");
                dialer.Dial();
                Console.WriteLine("Connected");
            }
            // Disconnect
            RasConnection conn = RasConnection.GetActiveConnections().Where(o => o.EntryName == "Your Entry (Connection Name)").FirstOrDefault();
            if (conn != null)
            {
                conn.HangUp();
                Console.WriteLine("Disconnected");
            }
            Console.ReadKey();
        }
    }
}

希望这会对某人有所帮助。