-1

I am a new guy in C# development and I am looking for a App to define a firewall rule... I searched nearly everywhere and found some code using NetFwMgr but it didn't work and I don't know why!? but I knew the cmd code for it, so I started to use the Process way in visual studio 2019.

the code line in cmd works fine and returns the word "OK." but I cant do it in C#

if anyone can help me understand the concept of it, I would be appreciate it.

here is the code snippet of what I have done:

 try
{
   string str = "netsh advfirewall firewall add rule name="+"\"Port for SQL\""+" dir=in action=allow 
                 protocol=TCP localport=1433";
   ProcessStartInfo psi = new ProcessStartInfo();
   Process process = new Process();

   psi.FileName = "cmd.exe";
   psi.WindowStyle = ProcessWindowStyle.Normal;
   psi.Verb = "runas";
   psi.Arguments = str;

   process.StartInfo = psi;
   process.Start();
   process.WaitForExit(1000);
}
catch (Exception ex)
{
     MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

the thing it does nothing. it simply waits for 1 second and nothing happens. it doesn't even catches any errors. Please Help

UPDATE : I even ran the exe file as administrator and nothing happens... I also gave the application manifest admin access:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

still no good

Atrin Noori
  • 311
  • 3
  • 12
  • You seem to be missing a space in your command arguments: `...name="+"\"Port for SQL\""+"dir=in...` should be `...name="+"\"Port for SQL\""+" dir=in...` – Peter Duniho May 15 '21 at 06:54
  • For future reference, Stack Overflow questions requesting debugging assistance are _required_ to include a proper [mcve]. The above fails on a number of counts, being not just incomplete, but also involving a command that requires privileged access to critical operating system settings which most people would not have any interest in modifying just for the purpose of reproducing your problem. It should have sufficed to provide an alternative command that still reproduces the problem; of course, in this case had you changed the command, the problem probably would have gone away, proving ... – Peter Duniho May 15 '21 at 06:57
  • ... that you don't actually have a useful programming question at all, but rather a typographical error in the command line arguments you're passing. – Peter Duniho May 15 '21 at 06:57
  • Does this answer your question? [Programmatically manage Windows Firewall](https://stackoverflow.com/questions/9471711/programmatically-manage-windows-firewall) – derpirscher May 15 '21 at 07:32
  • @derpirscher I actually tried it and I stated that i couldn't get an answer out of it, I saw the best thing to run batch code and the batch code runs in CMD but not in the Application – Atrin Noori May 15 '21 at 07:35
  • 1
    @Atrin Noori: Then you should inspect, what your command actually looks like (because like Peter Duniho said you are missing a space) and you should check the exit code of the process and also the stdout which probably would have given you a hint. – derpirscher May 15 '21 at 07:38
  • @derpirscher I did miss an space but I checked it out it wasnt the issue... cuz even with that missing space the code runs OK in cmd itself. now about your idea.. I checked the exit code in message box and it gave me negative number.. but I dont actually dont know how to use stdout – Atrin Noori May 15 '21 at 07:47
  • You can get the stdout from a proces like described here https://stackoverflow.com/questions/186822/capturing-console-output-from-a-net-application-c – derpirscher May 15 '21 at 07:59
  • @derpirscher thanks for the effort... but I did it and there was nothing a blank page of the console. – Atrin Noori May 15 '21 at 08:08
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232440/discussion-between-atrin-noori-and-derpirscher). – Atrin Noori May 15 '21 at 08:15

1 Answers1

1

You are calling cmd.exe and not netsh. The string you are passing as argument is not extecuted in the newly created console, you'd need to run cmd /c ... for that.

Just use netsh as your psi.Filename and advfirewall ... as your argument

string str = "advfirewall firewall add rule name=" + "\"Port for SQL\"" + " dir=in action=allow protocol=TCP localport=63000";
ProcessStartInfo psi = new ProcessStartInfo();
Process process = new Process();

psi.FileName = "netsh";
psi.WindowStyle = ProcessWindowStyle.Normal;
psi.Arguments = str;

process.StartInfo = psi;
process.Start();

process.WaitForExit();
derpirscher
  • 14,418
  • 3
  • 18
  • 35