本文介绍了如何在使用.NET安装期间为文件夹授予写入权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我创建了这样的安装程序类



first i created installer class like this

[RunInstaller(true)]
    public partial class Installer1 : System.Configuration.Install.Installer
    {
        public Installer1()
        {
            InitializeComponent();
        }
        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);
            SetDirectorySecurity();
        }

        private void SetDirectorySecurity()
        {
            string targetdir = Path.GetDirectoryName(Context.Parameters["Masterfinal"]);
            Log("Adding modify rights for " + targetdir);
            // The following code was copied (and modified) from "Jaso" (http://www.aspnet-answers.com/microsoft/NET-Security/30001760/how-to-change-group-permissions-on-existing-folder.aspx).
            // Retrieve the Directory Security descriptor for the directory
            var dSecurity = Directory.GetAccessControl(targetdir, AccessControlSections.Access);
            // Build a temp domainSID using the Null SID passed in as a SDDL string.
            // The constructor will accept the traditional notation or the SDDL notation interchangeably.
            var domainSid = new SecurityIdentifier("S-1-0-0");
            // Create a security Identifier for the BuiltinUsers Group to be passed to the new accessrule
            var ident = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, domainSid);
            // Create a new Access Rule.
            // ContainerInherit AND ObjectInherit covers both target folder, child folder and child object.
            // However, when using both (combined with AND), the permissions aren't applied.
            // So use two rules.
            // Propagate.none means child and grandchild objects inherit.
            var accessRule1 = new FileSystemAccessRule(ident, FileSystemRights.Modify, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow);
            var accessRule2 = new FileSystemAccessRule(ident, FileSystemRights.Modify, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow);
            // Add the access rules to the Directory Security Descriptor
            dSecurity.AddAccessRule(accessRule1);
            dSecurity.AddAccessRule(accessRule2);
            // Persist the Directory Security Descriptor to the directory
            Directory.SetAccessControl(targetdir, dSecurity);
            Log("Rights added.");
        }

        private void Log(string message)
        {
            Context.LogMessage(message);
            System.Diagnostics.Trace.WriteLine(message, "1177 client installer");
        }

    }
}







mysetupfile-> view-> customactions-> install->添加安装程序项目的输出



当我安装我的项目时出现错误1001参数null异常



哪里出错了?





什么是自定义动作属性?




mysetupfile->view->customactions->install->add output of installer project

when i am installing my project i got a error "1001 parameter null exception"

where am doing wrong?


what is custom action property?

推荐答案

private bool SetPermission(string applyPath)
        {
            try
            {
                DirectoryInfo IsRootAvailable = new DirectoryInfo(applyPath);
                if (IsRootAvailable.Exists)
                {
                    WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
                    FileSystemRights Rights = (FileSystemRights)0;
                    Rights = FileSystemRights.Modify;

                    // *** Add Access Rule to the actual directory itself
                    FileSystemAccessRule AccessRule = new FileSystemAccessRule("NETWORK SERVICE", Rights,
                                                InheritanceFlags.None,
                                                PropagationFlags.NoPropagateInherit,
                                                AccessControlType.Allow);
                    DirectoryInfo Info = new DirectoryInfo(applyPath);
                    DirectorySecurity Security = Info.GetAccessControl(AccessControlSections.Access);

                    bool Result = false;
                    Security.ModifyAccessRule(AccessControlModification.Set, AccessRule, out Result);
                    if (!Result)
                    {
                        rtxtConfirmMessage.Text += string.Format("\t\t\t--\t{0}", "Failed.");
                        return false;
                    }
                    // *** Always allow objects to inherit on a directory
                    InheritanceFlags iFlags = InheritanceFlags.ObjectInherit;
                    iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;

                    // *** Add Access rule for the inheritance
                    AccessRule = new FileSystemAccessRule("NETWORK SERVICE", Rights,
                                                iFlags,
                                                PropagationFlags.InheritOnly,
                                                AccessControlType.Allow);
                    Result = false;
                    Security.ModifyAccessRule(AccessControlModification.Add, AccessRule, out Result);
                    if (!Result)
                    {
                        rtxtConfirmMessage.Text += string.Format("\t\t\t--\t{0}", "Failed.");
                        return false;
                    }
                    Info.SetAccessControl(Security);
                    return true;
                }
                else
                    return false;
            }
            catch (Exception ex)
            {
                rtxtConfirmMessage.Text += string.Format("\t\t\t--\t{0}", "Failed." + ex.Message);
                return false;
            }
        }





和来自MSDN的帮助 []具有以下代码





and a help from MSDN FileSystemAccessRule Class[^] has the below code

using System;
using System.IO;
using System.Security.AccessControl;

namespace FileSystemExample
{
    class FileExample
    {
        public static void Main()
        {
            try
            {
                string fileName = "test.xml";

                Console.WriteLine("Adding access control entry for "
                    + fileName);

                // Add the access control entry to the file.
                AddFileSecurity(fileName, @"DomainName\AccountName",
                    FileSystemRights.ReadData, AccessControlType.Allow);

                Console.WriteLine("Removing access control entry from "
                    + fileName);

                // Remove the access control entry from the file.
                RemoveFileSecurity(fileName, @"DomainName\AccountName",
                    FileSystemRights.ReadData, AccessControlType.Allow);

                Console.WriteLine("Done.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        // Adds an ACL entry on the specified file for the specified account.
        public static void AddFileSecurity(string fileName, string account,
            FileSystemRights rights, AccessControlType controlType)
        {


            // Get a FileSecurity object that represents the
            // current security settings.
            FileSecurity fSecurity = File.GetAccessControl(fileName);

            // Add the FileSystemAccessRule to the security settings.
            fSecurity.AddAccessRule(new FileSystemAccessRule(account,
                rights, controlType));

            // Set the new access settings.
            File.SetAccessControl(fileName, fSecurity);

        }

        // Removes an ACL entry on the specified file for the specified account.
        public static void RemoveFileSecurity(string fileName, string account,
            FileSystemRights rights, AccessControlType controlType)
        {

            // Get a FileSecurity object that represents the
            // current security settings.
            FileSecurity fSecurity = File.GetAccessControl(fileName);

            // Remove the FileSystemAccessRule from the security settings.
            fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
                rights, controlType));

            // Set the new access settings.
            File.SetAccessControl(fileName, fSecurity);

        }
    }
}



这篇关于如何在使用.NET安装期间为文件夹授予写入权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 15:26