6

I'm making a WPF application where I use WebCLient to download file from a webserver. When I startup my app in debug mode it asks Firewall for permission and then I allow it to, which make my app works as it should.

But in release mode it does not prompt me with Firewall permission, which makes my app crash when downloading a file. I deploy my app in release mode. When I run the .exe file I get after deployment it still does not ask for Firewall permission which makes my app work incorrectly.

Is there a way to trigger Firewall prompt, maybe programmatically, in release mode?

SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
Loc Dai Le
  • 1,661
  • 4
  • 35
  • 70
  • This may be relevant : http://superuser.com/questions/115667/how-do-i-get-the-windows-7-firewall-to-prompt-me-whether-to-allow-or-deny-a-new – PaulF Aug 27 '15 at 12:09
  • I wonder why your get a Firewall prompt at all - WebClient uses an outgoing connection which by default is not blocked, so I suspect your application is crashing for another reason, what is the error? – markmnl Sep 09 '15 at 01:53

1 Answers1

0

Must read: I have searched for a way to request firewall access using C# but couldn't find anything relevant or safe..The batch is 100% clean and will not be detected as any sort of malware.(I couldn't find a way to do it in C# this is not off-topic!)

I don't know much about requesting to add an allow rule, but I know how to add a rule:
Just for inbound:

1) Start notepad and type:

@echo off
setlocal
set RULENAME="SomethingSpecial"
netsh advfirewall firewall show rule name=%RULENAME% >nul
if not ERRORLEVEL 1 ( goto :EOF) else (
netsh advfirewall firewall add rule name="SomethingSpecial" dir=in action=allow program="DIRECTORY" enable=yes) 
goto :EOF 

2) Replace SomethingSpecial with anything you want . It is just a name and if you replace it, replace it everywhere(you can keep SomethingSpecial as it is)

3) Replace DIRECTORY with your Release build directory (e.g: C:\program files\something.exe) dir means direction set to in because you are downloading. for both directions type this:

 @echo off
setlocal
set RULENAME="SomethingSpecial"
netsh advfirewall firewall show rule name=%RULENAME% >nul
if not ERRORLEVEL 1 ( goto :EOF) else (
netsh advfirewall firewall add rule name="SomethingSpecial" dir=in action=allow program="DIRECTORY" enable=yes
netsh advfirewall firewall add rule name="SomethingSpecial" dir=out action=allow program="DIRECTORY" enable=yes) 
goto :EOF

Save in .bat format and remember path

Now the fun part..

In your applications use below code .. If your program doesn't run as admin and you don't want Users to keep giving permissions find a way to run the process only once...Maybe save to a file, boolean or string and check it at program startup once so that the program doesn't run the bat process thing everytime.

 ProcessStartInfo bat = new ProcessStartInfo();
    bat.Verb = "runas";
    bat.FileName = "BatchDirectory";
    bat.WindowStyle = ProcessWindowStyle.Hidden;
    Process.Start(bat);

Replace BatchDirectory with your batch directory (use '/' not '\' just here)

Have fun! Comment if you have a question or if you have a problem.

  • I am struggling with this issue as well. I do not understand how your solution solved the problem. I added a rule by hand, but it didnt solve the problem – Travis Tubbs Jul 06 '17 at 23:09