Click here to Skip to main content
15,919,358 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
I need to write a .NET program to open ports through the NT firewall. Protocol (UDP or TCP) and port number will be passed by user as input.
Posted

Another way to do this is to use the netsh command. You would launch it as a process using Process.Start. This works on XP and Windows 7 (I assume Vista also because it works on 7).

 For example, if you wanted to open UDP port 20502, the netsh command would be:

 netsh firewall add portopening UDP 20502 UserFriendlyName ENABLE ALL

 And, if you wanted to close the port:

netsh firewall delete portopening UDP 20502 ALL

 

If you want to add a program exception to the firewall:

netsh firewall add allowedprogram "c:\program files\your folder\your.exe" UserFriendlyName

If you want to remove a program exception from the firewall: 

netsh firewall delete allowedprogram "c:\program files\your folder\your.exe"

 
Share this answer
 
v2
Have a look at the UdpClient and TcpClient classes.  The firewall needs to be configured manually to allow the ports that you want to use.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900