Bedford
Bedford

Reputation: 1186

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.

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:

  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?

Upvotes: 1

Views: 10850

Answers (3)

joekevinrayan96
joekevinrayan96

Reputation: 634

I know this question is old but could be useful for someone in the future, hence posting my answer here.

public class Program
{     
    static void Main(string[] args)
    {
        string command = "netsh interface set interface \"Ethernet\" enable";
        ProcessStartInfo processStartInfo = new ProcessStartInfo("CMD", command);
        Process proc = new Process();
        proc.StartInfo = processStartInfo;           
        proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
        proc.StartInfo.UseShellExecute = true;
        proc.StartInfo.Verb = "runas";            
        proc.StartInfo.Arguments = "/env /user:" + "Administrator" + " cmd /K"+ command;
        proc.StartInfo.CreateNoWindow = true;
        proc.Start();
        proc.WaitForExit();
    }
}

Ethernet is the name of the network interface. To get the name of the network interface you can simply type in any Command Prompt the following

netsh interface show interface

This works fine for me both with user account and admin account and approving admin permission is not required.

Upvotes: 1

Raged
Raged

Reputation: 404

Here is an example of some VB.NET code I am actually using in production:

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:

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

Upvotes: 2

fuzzybear
fuzzybear

Reputation: 2405

The Win32_NetworkAdapter class contains Enable/Disable methods 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 !

Upvotes: 3

Related Questions