如何在安装期间设置文件夹权限

时间:2017-11-16 09:38:09

标签: c# visual-studio installer

所以我创建了一个应用程序,用文本框文本填充.doc文件中的书签。 当我开发它时,一切都很好。

现在我正在尝试在安装过程之后调试它,如果我手动设置模板可以完全访问本地用户的文件夹,我会收到一个已解决的权限错误。

这些文件的排序方式如下:

ProgramFiles

     DefaultCompanyName

          ApplicationName

               WordTemplateLocalation#1

               WordTemplateLocalation#2

我想对这些文件夹及其子文件(即这些文件夹中的模板和其他资源)赋予修改和写入权限。

我已尝试过在StackOverflow和MSDN或任何其他博客中找到的很多内容,但我似乎无法完全理解如何创建自定义安装操作。< / p>

这是我最常遵循的内容: https://stackoverflow.com/a/10540927/6898247

但即使这样,它似乎也不起作用:https://stackoverflow.com/a/4392394/433718

甚至我也跟着MSDN演练:https://msdn.microsoft.com/en-us/library/d9k65z2d(v=vs.100).aspx

我试图从演练中添加DLL,但没有任何改变。

我不太确定我做错了什么。

一旦我创建了类似安装程序,我该怎么做:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
using System.Security.Principal;
using System.Security.AccessControl;

namespace Permissions_Setter
{
    [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);

            // This gets the named parameters passed in from your custom action
            string folder = Context.Parameters["folder"];

            // This gets the "Authenticated Users" group, no matter what it's called
            SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null);

            // Create the rules
            FileSystemAccessRule writerule = new FileSystemAccessRule(sid, FileSystemRights.Write, AccessControlType.Allow);

            if (!string.IsNullOrEmpty(folder) && Directory.Exists(folder))
            {
                // Get your file's ACL
                DirectorySecurity fsecurity = Directory.GetAccessControl(folder);

                // Add the new rule to the ACL
                fsecurity.AddAccessRule(writerule);

                // Set the ACL back to the file
                Directory.SetAccessControl(folder, fsecurity);
            }

            // Explicitly call the overriden method to properly return control to the installer
            /////////////////base.Install(stateSaver);
        }
        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);
        }
        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Rollback(IDictionary savedState)
        {
            base.Rollback(savedState);
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);
        }
    }
}

0 个答案:

没有答案