Autor Beitrag
epsodus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 38



BeitragVerfasst: Di 05.07.16 08:25 
Hallo zusammen,

ich habe mnir ein kleines Batchfile geschrieben, welches auch funktioniert. Dieses wurde ich gerne in mein C# Programm einbauen und mit einem Button ausführen.
Das Batchfile sieht wie folgt aus ( Ip-Adressen sind hier nur als Beispiel gesetzt und nicht existent):

@echo off
cls

netsh advfirewall firewall add rule name="Test Domains Block" dir=out interface=any
action=block remoteip=165.461.226.11,169.445.226.12,190.367.127.161,182.356.124.169

pause


ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
string parameters = @"netsh advfirewall firewall add rule " +
                                                 "name={0} " +
                                                 "dir={1} " +
                                                 "action={2} " +
                                                 "interface={3} " +
                                                 "remoteip={4}";
 
            string rulename = "Test Domains Block";
            string direction = "out"// in,out
            string action = "block"// allow,block,bypass
            string interface = "any";
            int remoteip = 165.461.226.11,169.445.226.12,190.367.127.161,182.356.124.169;
 
 
             ProcessStartInfo info = new ProcessStartInfo(@"C:\Windows\System32\netsh.exe");
             info.Arguments = String.Format(parameters, rulename, direction, action, interface, remoteip);
             var process = Process.Start(info);


Leider funktioniert meine obrige Annahme nicht. Kann mir hier jemand Hilfestellung geben ?
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Di 05.07.16 09:18 
Also ohne es ausprobiert zu haben, würde ich sagen, fehlen im C#-String "parameters" die Quotes beim name-Parameter und "netsh" gehört wiederum nicht dort rein.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
epsodus Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 38



BeitragVerfasst: Mi 06.07.16 09:45 
user profile iconChristian S. hat folgendes geschrieben Zum zitierten Posting springen:
Also ohne es ausprobiert zu haben, würde ich sagen, fehlen im C#-String "parameters" die Quotes beim name-Parameter und "netsh" gehört wiederum nicht dort rein.


Hallo, Programm funktioniert:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
string parameters = "advfirewall firewall add rule " + "name="{0}" + "dir={1" + "action={2" + "protocol={3" + "localport={4}";
 
            string rulename = "
TCP rule";
            string direction = "
in"; // in,out
            string action = "
block"; // allow,block,bypass
            string protocol = "
TCP"; // TCP, UDP
            int localport = 2300;
 
            ProcessStartInfo info = new ProcessStartInfo(@"
C:\Windows\System32\netsh.exe");
            info.Arguments = String.Format(parameters, rulename, direction, action, protocol, localport);
            info.UseShellExecute = false;
            var process = Process.Start(info);


Ich würde aber gerne statt protocol das Interface benutzen und statt localport eine IP-Adresse einsetzen.