7

我遇到了一个问题,我需要启用已禁用的卡,并且 WMI NetworkAdapter 上的搜索器不返回该对象。

我可以想到一种可能的方法来做到这一点,但我一直无法让它工作,那就是使用 this 作为构造函数名称创建一个 managementObject 。但这只会引发异常

{\\.\root\CIMV2:Win32_NetworkAdapter.NetConnectionID='Wireless Network Connection'}

另一种方法是对 netsh 进行 shell 并启用该设备,这有点难看,或者使用 shell32/dll "Enable" 来做同样的事情,同样,两者都只传递名称。我一直从注册表扫描中获取名称HKLM\SYSTEM\CurrentControlSet\Network并寻找 MediaType=2 以获取无线设备的字符串列表。如果我在启用适配器的情况下运行应用程序,一切都很好,因为我可以获得无线设备的 networkObject,但如果应用程序在无线设备被禁用时启动,这一切都会失败。

谢谢

编辑:这是我很想工作但不行的代码:(

using System;
using System.Management;
class Sample
{
    public static int Main(string[] args)
    {
        ManagementObject mObj = new ManagementObject("\\\\.\\root\\CIMV2:Win32_NetworkAdapter.NetConnectionID=\"Wireless Network Connection\"");
        mObj.InvokeMethod("Enable", null);
        return 0;
    }
}
4

2 回答 2

1

此方法本质上是使用 C# 来利用 WMI 和Win32_NetworkAdapterClass。它应该有内置的方法:

  • 使能够
  • 禁用

因此,您可以在 Selected 界面上执行命令。

您可以通过以下方式实现:

SelectQuery query = new SelectQuery("Win32_NetworkAdapter", "NetConnectionStatus=2");
ManagementObjectSearcher search = new ManagementObjectSearcher(query);
foreach(ManagementObject result in search.Get())
{
    NetworkAdapter adapter = new NetworkAdapter(result);

    // Identify the adapter you wish to disable here. 
    // In particular, check the AdapterType and 
    // Description properties.

    // Here, we're selecting the LAN adapters.
    if (adapter.AdapterType.Equals("Ethernet 802.3")) 
    {
        adapter.Disable();
    }
}

有一个博客实际上概述了这样的任务;它定义了如何围绕 WMI 类创建包装器。

另一种解决方案可能是也使用ControlServiceadvapi32)。

[DllImport("advapi32.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool    ControlService(IntPtr hService, SERVICE_CONTROL dwControl, ref SERVICE_STATUS lpServiceStatus);

希望其中一种方法有所帮助..

于 2013-04-01T20:44:33.250 回答
0
            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.Data;
            using System.Drawing;
            using System.Linq;
            using System.Text;
            using System.Windows.Forms;
            using System.Diagnostics;
            using System.Security.Principal;

            namespace WifiRouter
            {
                public partial class Form1 : Form
                {
                    bool connect = false;
                    public Form1()
                    {

                        InitializeComponent();
                    }

                    public static bool IsAdmin()
                    {
                        WindowsIdentity id = WindowsIdentity.GetCurrent();
                        WindowsPrincipal p = new WindowsPrincipal(id);
                        return p.IsInRole(WindowsBuiltInRole.Administrator);
                    }
                    public void RestartElevated()
                    {
                        ProcessStartInfo startInfo = new ProcessStartInfo();
                        startInfo.UseShellExecute = true;
                        startInfo.CreateNoWindow = true;
                        startInfo.WorkingDirectory = Environment.CurrentDirectory;
                        startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
                        startInfo.Verb = "runas";
                        try
                        {
                            Process p = Process.Start(startInfo);
                        }
                        catch
                        {

                        }

                        System.Windows.Forms.Application.Exit();
                    }
                    private void button1_Click(object sender, EventArgs e)
                    {
                        string ssid = textBox1.Text, key = textBox2.Text;
                        if (!connect)
                        {
                            if (String.IsNullOrEmpty(textBox1.Text))
                            {
                                MessageBox.Show("SSID cannot be left blank !",
                                "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            else
                            {

                                if (textBox2.Text == null || textBox2.Text == "")
                                {
                                    MessageBox.Show("Key value cannot be left blank !",
                                    "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                }
                                else
                                {
                                    if (key.Length >= 6)
                                    {
                                        Hotspot(ssid, key, true);
                                        textBox1.Enabled = false;
                                        textBox2.Enabled = false;
                                        button1.Text = "Stop";
                                        connect = true;
                                    }
                                    else
                                    {
                                        MessageBox.Show("Key should be more then or Equal to 6 Characters !",
                                        "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    }
                                }
                            }
                        }
                        else
                        {
                            Hotspot(null, null, false);
                            textBox1.Enabled = true;
                            textBox2.Enabled = true;
                            button1.Text = "Start";
                            connect = false;
                        }
                    }
                    private void Hotspot(string ssid, string key,bool status)
                    {
                        ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe");
                        processStartInfo.RedirectStandardInput = true;
                        processStartInfo.RedirectStandardOutput = true;
                        processStartInfo.CreateNoWindow = true;
                        processStartInfo.UseShellExecute = false;
                        Process process = Process.Start(processStartInfo);

                        if (process != null)
                        {
                            if (status)
                            {
                                process.StandardInput.WriteLine("netsh wlan set hostednetwork mode=allow ssid=" + ssid + " key=" + key);
                                process.StandardInput.WriteLine("netsh wlan start hosted network");
                                process.StandardInput.Close();
                            }
                            else
                            {
                                process.StandardInput.WriteLine("netsh wlan stop hostednetwork");
                                process.StandardInput.Close();
                            }
                        }
                    }

                    private void Form1_Load(object sender, EventArgs e)
                    {
                        if (!IsAdmin())
                        {
                            RestartElevated();
                        }
                    }

                    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
                    {
                        Hotspot(null, null, false);
                        Application.Exit();
                    }
                }
            }
于 2016-11-07T20:57:53.843 回答