c# 使用自写命令来一键控制无线和本地网络的开启关闭

程序需要用管理员的身份运行,使用WMI(Windows Management Instrumentation)

  • 使用ManagementObjectSearcher对象获取适配器信息
  • 使用ManagementObject的InvokeMethod方法执行相应操作
  • static void Main(string[] args)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("本地连接名称:Realtek Gaming GbE Family Controller");
                Console.WriteLine("无线连接名称:Intel(R) Wi-Fi 6 AX201 160MHz");
                Console.ForegroundColor = ConsoleColor.Green;
                string wx = "Intel(R) Wi-Fi 6 AX201 160MHz";
                string ben = "Realtek Gaming GbE Family Controller";
                string str = "编号1:无线开,本地关\r\n编号2:无线关,本地开";
                Console.WriteLine(str);
                Console.ForegroundColor = ConsoleColor.White;
                while (true)
                {
                    bool wxState = false;
                    bool benState = false;
                    string num = Console.ReadLine();
                    NetManager(num, wx, ben, ref wxState, ref benState);
                    Console.WriteLine("无线状态:" + wxState + "   本地状态:" + benState);
                    Console.ReadKey();
                }
            }
            /// 
            /// 获取网络
            /// 
            /// 标识
            /// 无线网络名称
            /// 本地网络名称
            /// 返回执行状态
            /// 返回执行状态
            public static void NetManager(string num,string wx,string ben, ref bool wxState, ref bool benState)
            {
                string manage = "SELECT * From Win32_NetworkAdapter";//  WHERE Name='本地连接'
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(manage);
                ManagementObjectCollection collection = searcher.Get();
                if (num == "1")
                {
                    foreach (ManagementObject obj in collection)
                    {
                        if (obj["Name"].ToString() == wx)
                        {
                            wxState= EnableLocalNetwork(obj);
                        }
                        else if (obj["Name"].ToString() == ben)
                        {
                            benState= DisableLocalNetwork(obj);
                        }
                    }
                }
                else if (num == "2")
                {
                    foreach (ManagementObject obj in collection)
                    {
                        if (obj["Name"].ToString() == wx)
                        {
                            wxState= DisableLocalNetwork(obj);
                        }
                        else if (obj["Name"].ToString() == ben)
                        {
                            benState= EnableLocalNetwork(obj);
                        }
                    }
                }          
            }
            /// 
            /// 禁用网卡
            /// 5
            /// 网卡对象
            /// 
            public static bool DisableLocalNetwork(ManagementObject adapter)
            {        
                if (adapter == null) return false;
                ManagementBaseObject inParams = adapter.GetMethodParameters("Disable");
                ManagementBaseObject outParams = adapter.InvokeMethod("Disable", inParams, null);
                uint resultCode = (uint)outParams["returnValue"];
                return resultCode == 0;
            }
            /// 
            /// 启用网卡
            /// 
            /// 网卡对象
            /// 
            public static bool EnableLocalNetwork(ManagementObject adapter)
            {          
                if (adapter == null) return false;
                ManagementBaseObject inParams = adapter.GetMethodParameters("Enable");
                ManagementBaseObject outParams = adapter.InvokeMethod("Enable", inParams, null);
                uint resultCode = (uint)outParams["returnValue"];
                return resultCode == 0;
            }
        }

你可能感兴趣的:(c#)