简体   繁体   English

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

[英]Disabling network adapter without admin rights prompt

I need to enable/disable all network adapters (kinda like Flight Mode) on a Windows 8 tablet when the user clicks on a button. 当用户单击按钮时,我需要在Windows 8平板电脑上启用/禁用所有网络适配器(类似于飞行模式)。

This can be done with the following cmdlet in Powershell: "Disable-NetAdapter * –Confirm:$false" and it's counterpart Enable-NetAdapter. 这可以通过Powershell中的以下cmdlet完成:“ Disable-NetAdapter * –Confirm:$ false”及其对应的Enable-NetAdapter。 They do exactly what I expect them to do, but I have two problems: 他们完全按照我的期望去做,但是我有两个问题:

  1. I don't want to run Powershell from the WPF application. 我不想从WPF应用程序运行Powershell。 Since it's built on the .NET Framework, is there any way to do the same without calling the cmdlet? 由于它是建立在.NET Framework之上的,因此无需调用cmdlet,有没有办法做到这一点?

  2. It requires elevated rights (like starting the app with Right Click +"Run as Administrator"). 它需要提升的权限(例如,通过右键单击+“以管理员身份运行”启动应用程序)。 I can get the elevated permissions from code, but I always get the User Access Control popup asking for approval. 我可以从代码中获得提升的权限,但是我总是得到“用户访问控制”弹出窗口,要求批准。 Is there a way to always start an application with elevated rights without getting the popup? 有没有一种方法可以始终以提升的权限启动应用程序而不会弹出窗口?

The Win32_NetworkAdapter class contains Enable/Disable methods http://msdn.microsoft.com/en-us/library/aa394216 Win32_NetworkAdapter类包含启用/禁用方法http://msdn.microsoft.com/zh-cn/library/aa394216

here's a code example from Programmatically Enable / Disable Connection 这是通过编程启用/禁用连接的代码示例

You need to run in Admin or System context if the operation requires them, ideally as System as UAC does not get in way, you could run as service ! 如果操作需要它们,则需要在Admin或System上下文中运行,理想情况下,由于UAC不会妨碍System,您可以作为服务运行!

Here is an example of some VB.NET code I am actually using in production: 这是我实际上在生产中使用的一些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

EDIT: Adding C# equivalent: 编辑:添加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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM