次の方法で共有


Adding a URL Exemption in c#

The system contains a list of applications which are exempted from parental controls.  This list can be accessed via WMI and it should be modified in the install process for any application that requires free Internet access.  The way to do this is to access the class via WMI and add the specified program into the list of app's.  Here is an example written in C# to do just this.

There are two properties on the WMI object which have to do with the application exemption list.  There is the HTTPExemptionList and the WinHTTPExemptionList, the HTTPExemption list is the one that is used to add in extra programs.  The WinHTTPExemptionList is a readonly property that cannot be changed, it has some internal system exemptions.

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace AddUrlExemption
{
    /// <summary>
    /// Class which has all the system settings for WPC stored in it.
    /// </summary>
    public class SystemSettings
    {
        private ManagementObject _systemSettings;

        /// <summary>
        /// Default constructor, makes a new system settings WMI object.
        /// </summary>
        public SystemSettings()
        {
            _systemSettings = new ManagementObject(
                    @"root\CIMV2\Applications\WindowsParentalControls",
                    "WpcSystemSettings=@",
                    new ObjectGetOptions());
        }

        /// <summary>
        /// The application overrides in existance on the system. THis allows the
        /// application overrides to be set and retrieved.
        /// </summary>
        public List<string> AppOverrides
        {
            get
            {
                string[] urls = (string[])_systemSettings["HTTPExemptionList"];
                List<string> ret = new List<string>();
                foreach (string url in urls)
                {
                    ret.Add(url);
                }
                return ret;
            }
            set
            {
                string[] setting = new string[value.Count];
                int pos = 0;
                foreach (string url in value)
                {
                    setting[pos] = url;
                    pos++;
                }
                _systemSettings["HTTPExemptionList"] = setting;
            }
        }

        /// <summary>
        /// Saves the system settings back into the system.
        /// </summary>
        /// <returns>the management path of the saved object</returns>
        public ManagementPath Save()
        {
            return _systemSettings.Put();
        }
    }

    /// <summary>
    /// Main program to call the class.
    /// </summary>
    class Program
    {

        /// <summary>
        /// Prints the string list of app overrides.
        /// </summary>
        /// <param name="list">the list to print</param>
        static void PrintAppOverrides(List<string> list)
        {
            foreach (string s in list)
            {
                System.Console.WriteLine(" " + s);
            }
        }

        /// <summary>
        /// Adds in a new override into the list.
        /// </summary>
        /// <param name="args">the command line args</param>
        static void Main(string[] args)
        {
            SystemSettings setting = new SystemSettings();
            List<string> appOverrides = setting.AppOverrides;
            System.Console.WriteLine("Start Overrides:");
            PrintAppOverrides(appOverrides);
            appOverrides.Add("c:\\windows\\bfsrv.exe");
            setting.AppOverrides = appOverrides;
            setting.Save();
            System.Console.WriteLine("Added: c:\\windows\\bfsrv.exe");
        }
    }
}