【问题标题】:C# WMI: Throws an error when I try to enable/disable PPPoE adapterC# WMI:当我尝试启用/禁用 PPPoE 适配器时引发错误
【发布时间】:2016-05-11 11:34:45
【问题描述】:

我正在尝试根据this answer 启用/禁用我的 PPPoE 适配器。
它适用于普通适配器,但不适用于 PPPoE,它会引发错误:

查询 WMI 数据时出错:方法参数无效

适配器名称正确我为此目的使用了 WMI 查询工具,但我不知道需要设置哪些参数。任何帮助将不胜感激。

编辑

这是我使用的代码:

    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();
    }

【问题讨论】:

    标签: c# wmi


    【解决方案1】:

    好的,我已经找到了 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();
            }
        }
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-03
      • 2013-10-06
      • 1970-01-01
      • 1970-01-01
      • 2022-11-29
      • 1970-01-01
      相关资源
      最近更新 更多