Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# get or create firewall inbound rule ports

using NetFwTypeLib; // Located in FirewallAPI.dll
...
INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance(
    Type.GetTypeFromProgID("HNetCfg.FWRule"));
firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_BLOCK;
firewallRule.Description = "Used to block all internet access.";
firewallRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT;
firewallRule.Enabled = true;
firewallRule.InterfaceTypes = "All";
firewallRule.Name = "Block Internet";

INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(
    Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Add(firewallRule);
-------------
 using System.Management.Automation;
...
private void OpenPort(int port)
{
    var powershell = PowerShell.Create();
    var psCommand = $"New-NetFirewallRule -DisplayName "<rule description>" -Direction Inbound -LocalPort {port} -Protocol TCP -Action Allow";
    powershell.Commands.AddScript(psCommand);
    powershell.Invoke();
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to make a console feedback 
Csharp :: user control equivalent event for form.shown c# 
Csharp :: Delegate no parameter no return 
Csharp :: get all controlswpf 
Csharp :: linked list follow what in c# 
Csharp :: generate random light color android 
Csharp :: C# data base sql 
Csharp :: system.collections.generic.list 1 system.int32 c# 
Csharp :: tmpro pageCount update 
Csharp :: c# alert message 
Csharp :: search list for words c# 
Csharp :: exception meaning in .net core 
Csharp :: how to move an object with addforce 
Csharp :: single or default in c# 
Csharp :: querstring fromat asp.net c# 
Csharp :: c# with keyword 
Csharp :: OBSERVER 
Csharp :: Unity Wait Time Fixed 
Csharp :: get all viewsheet revit api 
Csharp :: What is the best way to lock cache in asp.net? 
Csharp :: asp net identity login failed for user 
Csharp :: c# get Full Exception message if InnerException is not NULL 
Csharp :: print the top view of the binary tree 
Csharp :: c# read key without writing 
Csharp :: what is vector3.one c# 
Csharp :: php encrypt message encrypt() decrypt 
Csharp :: slider script unity 
Csharp :: asp.net core reverse engineer database 
Csharp :: how to get user control dropdown value in asp net 
Csharp :: cs foreach int 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =