Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ebubekirbastama/7627e24c0844fc486aac12ab201e01c2 to your computer and use it in GitHub Desktop.
Save ebubekirbastama/7627e24c0844fc486aac12ab201e01c2 to your computer and use it in GitHub Desktop.
using NetFwTypeLib;
Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2);
var currentProfiles = fwPolicy2.CurrentProfileTypes;
INetFwRule firewallRule = fwPolicy2.Rules.OfType<INetFwRule>().Where(
x => x.Name == textBoxX2.Text)
.FirstOrDefault(); // Eklemek istediğiniz kuralın kayıtlı olup olmadığını kontrol ediyoruz
if (firewallRule == null)
{
INetFwRule2 inboundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
inboundRule.Enabled = true;
inboundRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
inboundRule.Protocol = 6; // TCP Protokol Numarası
inboundRule.LocalPorts = textBoxX1.Text; // Açmak istediğiniz port
inboundRule.Name = textBoxX2.Text; // Kural ismi
inboundRule.ApplicationName = textBoxX3.Text;
inboundRule.Description = richTextBox1.Text;
inboundRule.Profiles = currentProfiles;
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Add(inboundRule);
MessageBox.Show("Kural Başarılı Bir Şekilde Oluşturuldu.");
}
else
{
MessageBox.Show("Bu Kural Mevcut.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment