vs2008 安装程序 office2003

最需要注意的是增加一个SetSecurity的类库项目,再增加一个安装程序类,例如名字叫SetSecurity.cs,

代码如下:


private const string ALL_GROUP_NAME = "AdManager_{3B5BD734-E998-4601-A838-A84575EB0A77}";

        public SetSecurity()
        {
            InitializeComponent();
        }


        public override void Install(System.Collections.IDictionary stateSaver)
        {

            PolicyLevel ent;

            PolicyLevel mach;

            PolicyLevel user;
            // 这里获取参数
            string custPath = Context.Parameters["custpath"];
            //这里将相关的dll都增加进去
            string[] assemblies = new string[] { "blueoutlook03.dll", "Newtonsoft.Json.dll", "component.dll"};



            IEnumerator policyEnumerator = SecurityManager.PolicyHierarchy();

            policyEnumerator.MoveNext();

            ent = (PolicyLevel)policyEnumerator.Current;

            policyEnumerator.MoveNext();

            mach = (PolicyLevel)policyEnumerator.Current;

            policyEnumerator.MoveNext();

            user = (PolicyLevel)policyEnumerator.Current;



            PermissionSet nothingTrust = user.GetNamedPermissionSet("Nothing");

            PolicyStatement nothingPolicy = new PolicyStatement(nothingTrust, PolicyStatementAttribute.Nothing);

            AllMembershipCondition allCondition = new AllMembershipCondition();

            CodeGroup allGroup = new UnionCodeGroup(allCondition, nothingPolicy);

            allGroup.Name = ALL_GROUP_NAME;



            PermissionSet executionTrust = user.GetNamedPermissionSet("Execution");

            PolicyStatement executionPolicy = new PolicyStatement(executionTrust, PolicyStatementAttribute.Nothing);

            UrlMembershipCondition pathCondition = new UrlMembershipCondition(custPath + "*");

            CodeGroup pathGroup = new UnionCodeGroup(pathCondition, executionPolicy);

            pathGroup.Name = custPath;



            foreach (string assembly in assemblies)
            {

                PermissionSet fullTrust = user.GetNamedPermissionSet("FullTrust");

                PolicyStatement assemblyPolicy = new PolicyStatement(fullTrust, PolicyStatementAttribute.Nothing);

                UrlMembershipCondition condition = new UrlMembershipCondition(custPath + assembly);

                CodeGroup assemblyGroup = new UnionCodeGroup(condition, assemblyPolicy);

                assemblyGroup.Name = assembly;

                pathGroup.AddChild(assemblyGroup);

            }



            allGroup.AddChild(pathGroup);

            user.RootCodeGroup.AddChild(allGroup);

            SecurityManager.SavePolicy();

            base.Install(stateSaver);

        }



        public override void Uninstall(IDictionary savedState)
        {

            DeletePrivilege();



            base.Uninstall(savedState);

        }



        public override void Rollback(IDictionary savedState)
        {

            DeletePrivilege();



            base.Rollback(savedState);

        }



        private void DeletePrivilege()
        {

            PolicyLevel ent;

            PolicyLevel mach;

            PolicyLevel user;



            IEnumerator policyEnumerator = SecurityManager.PolicyHierarchy();

            policyEnumerator.MoveNext();

            ent = (PolicyLevel)policyEnumerator.Current;

            policyEnumerator.MoveNext();

            mach = (PolicyLevel)policyEnumerator.Current;

            policyEnumerator.MoveNext();

            user = (PolicyLevel)policyEnumerator.Current;



            CodeGroup allGroup = null;

            foreach (CodeGroup group in user.RootCodeGroup.Children)
            {

                if (group.Name == ALL_GROUP_NAME)
                {

                    allGroup = group;

                    break;

                }

            }



            if (allGroup != null)
            {

                user.RootCodeGroup.RemoveChild(allGroup);

                SecurityManager.SavePolicy();

            }

        }