0

So writing a C# program which will require network access, thus needing to add a rule to Windows Firewall.

Here is the code:

Console.WriteLine("Firewall not yet configured, doing now.");
PowerShell allowtorthroughwindowsfirewallinbound = PowerShell.Create("New - NetFirewallRule - DisplayName ""Allow TOR through Windows Firewall"" - Direction Inbound - Program ""C:\\ProgramData\\chocolatey\\bin\\tor.exe"" - Action Allow;')");
allowtorthroughwindowsfirewallinbound.Invoke();

Problem though,the "create" throws the following error:

Severity    Code    Description Project File    Line    Suppression State
Error   CS1501  No overload for method 'Create' takes 5 arguments   TorRunner   C:\TorRunner\TorRunner\Program.cs   64  Active

I can only assume the string is formatted incorrectly, so what would be the correct string format for creating a firewall string that would add a windows firewall rule with a name?

Thanks.

6
  • Put that string into a Console.WriteLine and keep fixing the escaping of " until it gives you an output which looks as expected. Then that fixed string should work here. Commented May 3, 2024 at 17:31
  • The following may be of interest: stackoverflow.com/questions/78414068/…, stackoverflow.com/questions/15409790/…, stackoverflow.com/a/76398401/10024425, Commented May 3, 2024 at 17:55
  • 1
    Can you do it without using PowerShell? (A: Yes.) Commented May 3, 2024 at 18:45
  • at the end of string dont need ') Commented May 3, 2024 at 19:37
  • Argument 1: cannot convert from 'string' to 'System.Management.Automation.RunspaceMode' Commented May 4, 2024 at 3:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.