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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22617712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:20:02  来源:igfitidea点击:

Disabling network adapter without admin rights prompt

c#wpfpowershell

提问by Bedford

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. They do exactly what I expect them to do, but I have two problems:

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

  1. I don't want to run Powershell from the WPF application. Since it's built on the .NET Framework, is there any way to do the same without calling the 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?

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

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

回答by saj

The Win32_NetworkAdapter class contains Enable/Disable methods http://msdn.microsoft.com/en-us/library/aa394216

Win32_NetworkAdapter 类包含启用/禁用方法http://msdn.microsoft.com/en-us/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 !

如果操作需要它们,您需要在管理员或系统上下文中运行,理想情况下,因为 UAC 不会妨碍系统,您可以作为服务运行!

回答by Raged

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