question

Jackson1990-7147 avatar image
0 Votes"
Jackson1990-7147 asked Jackson1990-7147 commented

Rule is not created expectedly

Hi,
What is wrong below? I want to call Command prompt (by running it as Administrator) and further apply firewall rule. It seems relevant rule is not created expectedly. Why?

                                             fld1 = "netsh advfirewall firewall add rule name=\"Rule " + DateTime.Now.ToString() + "_" + Convert.ToString(cnt3).Trim() + "\" dir=in action=block remoteip=" + fld0 + ".1.1-" + fld0 + ".255.255";
                                             System.Diagnostics.Process.Start("CMD.exe", fld1);


dotnet-csharpvs-generaldotnet-visual-basicwindows-forms
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks. fld1 is having relevant rule details inside. But the rule is not created expectedly by the line below. Why?
System.Diagnostics.Process.Start("CMD.exe", fld1);

0 Votes 0 ·

Hi @Jackson1990-7147,
The arguments parameter must include either a "/c" or "/k" argument to specify whether the command window exits or remains after completion.
And I re-edited my answer you can refer to.
Best Regards,
Daniel Zhang

0 Votes 0 ·
DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered DanielZhang-MSFT edited

Hi Jackson1990-7147,
Based on your code, you need to change "action=block" to "action=allow".
Then you need to pass an argument to cmd.exe via following code:

 System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo();
 proc.FileName = "CMD.exe";
 proc.Arguments ="/k "+f1d1;
 System.Diagnostics.Process.Start(proc);

For more information about how to add firewall rules, run the following command:

 netsh advfirewall firewall add rule ?

And here is a useful document you can refer to.
Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Jackson1990-7147 avatar image
0 Votes"
Jackson1990-7147 answered Jackson1990-7147 commented

Many thanks.
Is there a way to ensure that CMD process would be closed after being run, as there would be many pending CMD session, if I have a looping to generate that?

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You can add Rules by code, with INetFwRules.Add (P/Invoke or NetFwTypeLib in .NET)


0 Votes 0 ·

Hi,
Can you share your way with more details?

0 Votes 0 ·

Hi,
Can I have more details to such way?

Many Thanks & Best Regards,
Jackson

0 Votes 0 ·