C
C#12mo ago
LordoToasty

"netsh" cmd Commands not being executed

Well, I am trying to set up some firewall-rules and such, the problem is non of the commands is being properly executed. I got to the conclusion while looking for why the server does not work. My approach was to try to delete the firewall rule after running the program, as of proof for that it existed in the first place, it turned out, it didn't, my code is:
5 Replies
LordoToasty
LordoToastyOP12mo ago
Terminal output: PortNum:25565 Local_IPAddress_Probe1: 192.168.178.36 Local_IPAddress_Probe2: 192.168.178.36
jcotton42
jcotton4212mo ago
does running those commands directly in cmd work? also, is your app running in the background? b/c Verb = RunAs will trigger UAC
LordoToasty
LordoToastyOP12mo ago
Allright! I found a solution after ages, here is the working code now, tho if anyone knows how to make you only have to click the "ok" button once, I would love to hear that as it is really annoying to have to give the app a administrative-level multiple times over. Working code:
jcotton42
jcotton4212mo ago
tho if anyone knows how to make you only have to click the "ok" button once
you would have to put all the command calls into one Process also, protip, use string interpolation instead of concat, eg
string firewallcommand = $"netsh advfirewall firewall add rule {firewallrulename} dir=in action=allow protocol=TCP localport={targetport}";
string firewallcommand = $"netsh advfirewall firewall add rule {firewallrulename} dir=in action=allow protocol=TCP localport={targetport}";
in cmd you can run multiple commands by separating them with &&
string command = $"""
netsh advfirewall firewall add rule {firewallrulename} dir=in action=allow protocol=TCP localport={targetport} && ^
netsh interface portproxy add v4tov4 {targetport} {localIpAddress} {targetport} 0.0.0.0
""";
string command = $"""
netsh advfirewall firewall add rule {firewallrulename} dir=in action=allow protocol=TCP localport={targetport} && ^
netsh interface portproxy add v4tov4 {targetport} {localIpAddress} {targetport} 0.0.0.0
""";
something like that raw interpolated string the ^ tells cmd to ignore the newline, so you can split the command across multiple lines
LordoToasty
LordoToastyOP12mo ago
Cool, Thaks a lot for the Tip, I'll try to remember!
Want results from more Discord servers?
Add your server