禁用没有管理员权限提示的网络适配器

时间:2014-03-24 18:29:49

标签: c# wpf powershell

当用户点击按钮时,我需要在Windows 8平板电脑上启用/禁用所有网络适配器(有点像飞行模式)。

这可以通过Powershell中的以下cmdlet完成:“Disable-NetAdapter * -Confirm:$ false”,它是对应的Enable-NetAdapter。他们完全按照我的期望去做,但我有两个问题:

  1. 我不想从WPF应用程序运行Powershell。由于它是基于.NET Framework构建的,有没有办法在不调用cmdlet的情况下执行相同操作?

  2. 它需要提升的权限(例如使用右键单击+“以管理员身份运行”启动应用程序)。我可以从代码获得提升的权限,但我总是得到用户访问控制弹出窗口,要求批准。有没有办法在不获取弹出窗口的情况下始终启动具有提升权限的应用程序?

2 个答案:

答案 0 :(得分:3)

Win32_NetworkAdapter类包含启用/禁用方法http://msdn.microsoft.com/en-us/library/aa394216

这是Programmatically Enable / Disable Connection

的代码示例

如果操作需要,您需要在管理员或系统上下文中运行,理想情况下,系统与UAC不相关,您可以作为服务运行!

答案 1 :(得分:2)

以下是我实际在生产中使用的一些VB.NET代码的示例:

Imports System.Management
Imports System.Text.RegularExpressions

            Try
                Dim scope As New ManagementScope("\\" + computername + "\root\CIMV2")
                scope.Connect()

                Dim query As New ObjectQuery( _
                    "SELECT * FROM Win32_NetworkAdapter WHERE Manufacturer != 'Microsoft' AND NOT PNPDeviceID LIKE 'ROOT\\%'")

                Dim searcher As New ManagementObjectSearcher(scope, query)

                For Each queryObj As ManagementObject In searcher.Get()

                    Dim ServiceName As String = queryObj("ServiceName")
                    Dim ProductName As String = queryObj("Description")
                    If Regex.IsMatch(ServiceName, ".*NETw.*") Then
                        'if we detect a wireless connection service name...

                        If Regex.IsMatch(queryObj("netenabled"), ".*true.*", RegexOptions.IgnoreCase) Then                                
                           MessageBox.Show(ProductName + " is already enabled! [ " + queryObj("netenabled") + " ]")

                        Else
                            'Try to enable the wireless connection here
                            queryObj.InvokeMethod("Enable", Nothing)                                
                                MessageBox.Show(ProductName + " was successfully enabled!")                               
                        End If
                    End If
                Next
            Catch ex As Exception
                Messagebox.show(ex.Message)
            End Try

编辑:添加C#等价物:

try {
ManagementScope scope = new ManagementScope("\\\\" + computername + "\\root\\CIMV2");
scope.Connect();

ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_NetworkAdapter WHERE Manufacturer != 'Microsoft' AND NOT PNPDeviceID LIKE 'ROOT\\\\%'");

ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);


foreach (ManagementObject queryObj in searcher.Get()) {
    string ServiceName = queryObj("ServiceName");
    string ProductName = queryObj("Description");
    if (Regex.IsMatch(ServiceName, ".*NETw.*")) {
        //if we detect a wireless connection service name...

        if (Regex.IsMatch(queryObj("netenabled"), ".*true.*", RegexOptions.IgnoreCase)) {
            MessageBox.Show(ProductName + " is already enabled! [ " + queryObj("netenabled") + " ]");

        } else {
            //Try to enable the wireless connection here
            queryObj.InvokeMethod("Enable", null);
            MessageBox.Show(ProductName + " was successfully enabled!");
        }
    }
}
} catch (Exception ex) {
Messagebox.show(ex.Message);
}