/// <summary>
        ///     Creates a new application rule for Windows Firewall v1
        /// </summary>
        /// <param name="name">Name of the rule</param>
        /// <param name="processAddress">Address of the executable file</param>
        /// <param name="profiles">The profile that this rule belongs to</param>
        public FirewallLegacyApplicationRule(string name, string processAddress, FirewallProfiles profiles)
        {
            if (profiles.HasFlag(FirewallProfiles.Public))
            {
                throw new FirewallLegacyNotSupportedException(
                          "Public profile is not supported when working with Windows Firewall Legacy."
                          );
            }

            UnderlyingObjects = new Dictionary <FirewallProfiles, INetFwAuthorizedApplication[]>();

            foreach (var profile in Enum.GetValues(typeof(FirewallProfiles)).OfType <FirewallProfiles>())
            {
                if (profiles.HasFlag(profile))
                {
                    UnderlyingObjects.Add(
                        profile,
                        new[] { new INetFwAuthorizedApplication() }
                        );
                }
            }

            if (UnderlyingObjects.Count == 0)
            {
                throw new ArgumentException("At least one profile is required.", nameof(profiles));
            }

            Name            = name;
            ApplicationName = processAddress;
            IsEnable        = true;
            Scope           = FirewallScope.All;
            IsEnable        = true;
        }
        /// <summary>
        ///     Creates a new port rule for Windows Firewall v1
        /// </summary>
        /// <param name="name">Name of the rule</param>
        /// <param name="port">Port number of the rule</param>
        /// <param name="profiles">The profiles that this rule belongs to</param>
        /// <param name="typeResolver">The COM+ object resolver</param>
        public FirewallLegacyPortRule(string name, ushort port, FirewallProfiles profiles, COMTypeResolver typeResolver)
        {
            TypeResolver = typeResolver;

            if (profiles.HasFlag(FirewallProfiles.Public))
            {
                throw new FirewallLegacyNotSupportedException(
                          "Public profile is not supported when working with Windows Firewall Legacy."
                          );
            }

            UnderlyingObjects = new Dictionary <FirewallProfiles, INetFwOpenPort[]>();

            foreach (var profile in Enum.GetValues(typeof(FirewallProfiles)).OfType <FirewallProfiles>())
            {
                if (profiles.HasFlag(profile))
                {
                    UnderlyingObjects.Add(
                        profile,
                        new[] { typeResolver.CreateInstance <INetFwOpenPort>() }
                        );
                }
            }

            if (UnderlyingObjects.Count == 0)
            {
                throw new ArgumentException("At least one profile is required.", nameof(profiles));
            }

            Name      = name;
            LocalPort = port;
            IsEnable  = true;
            Scope     = FirewallScope.All;
            IsEnable  = true;
        }
Exemple #3
0
        /// <summary>
        ///     Creates a new application rule for Windows Firewall v1
        /// </summary>
        /// <param name="name">Name of the rule</param>
        /// <param name="processAddress">Address of the executable file</param>
        /// <param name="profiles">The profile that this rule belongs to</param>
        public FirewallLegacyApplicationRule(string name, string processAddress, FirewallProfiles profiles)
        {
            if (profiles.HasFlag(FirewallProfiles.Public))
            {
                throw new FirewallLegacyNotSupportedException("Public profile is not supported.");
            }

            UnderlyingObjects = new Dictionary <FirewallProfiles, INetFwAuthorizedApplication>();
            foreach (var profile in Enum.GetValues(typeof(FirewallProfiles)).OfType <FirewallProfiles>())
            {
                if (profiles.HasFlag(profile))
                {
                    UnderlyingObjects.Add(
                        profile,
                        ComHelper.CreateInstance <INetFwAuthorizedApplication>()
                        );
                }
            }

            if (UnderlyingObjects.Count == 0)
            {
                throw new ArgumentException("At least one profile is required.", nameof(profiles));
            }

            Name = name;
            ExecutableAddress = processAddress;
            IsEnable          = true;
            Scope             = FirewallScope.All;
            IsEnable          = true;
        }