Rule is not created expectedly

Peter_1985 2,466 Reputation points
2021-01-25T07:00:30.773+00:00

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);
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,821 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,575 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,199 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,564 questions
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,611 Reputation points
    2021-01-26T05:26:21.367+00:00

    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.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Peter_1985 2,466 Reputation points
    2021-01-26T13:04:26.863+00:00

    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?