Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# to run cmd netsh

I am trying to create a c# form with 2 buttons.

btn1 = disable LAN
btn2 = enable LAN

I have never done something on this level before.

What I want to accomplish is when I click on btn1 I want to disabled the LAN, and btn to enable it again.

This is what I am testing:

System.Diagnostics.Process.Start(ipconfig);

This runs, it runs the cmd and ipconfig I can actually see the IP display BUT...

This is the netsh command:

netsh interface set interface name="Local Area Connection" admin=DISABLED

If I run this via the console it works 100%

I want to use this command by clicking the button?

How do I go about getting this to work? Because I have tried this and does not work:

System.Diagnostics.Process.Start(netsh interface set interface name="Local Area Connection " admin=DISABLED);

any help?

like image 1000
s0mePe0ple Avatar asked Apr 15 '26 14:04

s0mePe0ple


2 Answers

This should work:

System.Diagnostics.Process.Start("netsh.exe", "whatever you would need to write as parameters");

For instance, for a mobile broadband connection, I would start this connection with:

System.Diagnostics.Process.Start("netsh.exe", "mbn connect interface=\"Mobile broadband\" connmode=name name=\"NAME OF CONNECTION\"");

In the above example, you would be able to find the name of the connection by finding the connection in your typical network manager (in Windows 8, it's in the right-side slide-in network overview thing). This part is not really relevant to your question, though, but others might be interested in it.

like image 147
markj Avatar answered Apr 17 '26 03:04

markj


I had a similar problem that was fixed with this solution:

Process.Start("powershell.exe", "netsh interface set interface name=\"Local Area Connection\" admin=DISABLED");

With this method, the command is executed in PowerShell

like image 36
AliT Avatar answered Apr 17 '26 05:04

AliT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!