Rule is not created expectedly

Peter_1985 2,866 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);
Developer technologies | Windows Forms
Developer technologies | VB
Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
Developer technologies | C#
Developer technologies | 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.
{count} votes

Answer accepted by question author
  1. Daniel Zhang-MSFT 9,661 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,866 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?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.