如何在使用.NET安装期间为文件夹提供读/写权限

Jed*_*Jed 28 .net c# windows-installer file-permissions

我有一个使用Visual Studio 2010构建的安装项目.

安装程序在将应用程序及其所有依赖项安装到正确的子目录和程序数据目录方面工作正常.

但是,我注意到安装程序创建的每个目录(根文件夹及其所有子目录)都不提供"写入"权限.添加到"用户"组目录的唯一权限是:

  • 阅读并执行
  • 列出文件夹内容

无论用户是否将应用程序安装为"管理员",都会发生这种明显的默认权限设置.

对我来说,安装程序没有给正在安装的应用程序使用的文件夹赋予"写入"权限,这似乎很奇怪 - 安装程序在ProgramData应用程序数据库的文件夹中创建的文件夹更加令人困惑.获得"写"权限.

我的问题是,有没有办法配置安装项目,以便在创建文件夹时,我们可以告诉它提供什么类型的权限以及向谁提供.在我的情况下,我需要提供(应用程序的)根目录及其所有子目录,以及放置在ProgramData"用户组"的"读/写"文件夹文件夹中的文件夹.从技术上讲,我很乐意向"用户组"提供"完全控制"目录.

ACK*_*low 36

我想我的其他帖子因为有点过于笼统而被删除了,所以我在下面对它进行了改进:

要做的是做一个自定义动作.这非常简单,请查看MSDN演练,以便在此处编写C#自定义操作.您将更改权限的代码放在Install方法中:

按照链接中的前几个步骤获取从安装程序解决方案引用的新安装程序项目.你必须这样做,所以你可以构建一个在安装结束时调用的dll.

实际上为用户设置读/写权限有点棘手,我能得到的最接近的是为Authenticated Users设置.我拼凑了一些我在互联网上找到的其他解决方案来提出这个问题:

public override void Install(IDictionary 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);
}
Run Code Online (Sandbox Code Playgroud)

然后,在创建自定义操作时,编辑其属性,并在CustomActionData属性下添加类似的内容:

/folder="[CommonAppDataFolder][ProductName]"
Run Code Online (Sandbox Code Playgroud)

  • 确认信息[来自你的帖子] _"和我能得到的最接近的是为认证用户设置"_.有一个**BuiltinUsersSid**代表**Users**系列. (2认同)
  • 我必须添加一些权利才能让它正常运行.我使用了代码示例中发布的权利http://stackoverflow.com/a/4392394/433718 (2认同)

Cos*_*rvu 11

默认情况下,用户组在程序文件等每个计算机位置中没有写访问权限.这是与安装无关的Windows标准.但是,在安装期间,您可以设置所需的任何权限.

Windows Installer支持自定义权限,但Visual Studio不提供设置它们的方法.因此,Visual Studio中唯一的解决方案是自定义操作.

不幸的是,Visual Studio不支持附加的自定义操作.因此,使用XCACLS.EXE设置权限只有在将其包含在程序包中时才会起作用(它将与文件一起安装在目标计算机上).

更简洁但更复杂的解决方案是自己编写自定义操作(使用自定义代码)来设置所需的权限.

最快,最干净的解决方案是使用不同的设置创作工具,提供更多的权限控制.


小智 9

private static void GrantAccess(string file)
        {
            bool exists = System.IO.Directory.Exists(file);
            if (!exists)
            {
                DirectoryInfo di = System.IO.Directory.CreateDirectory(file);
                Console.WriteLine("The Folder is created Sucessfully");
            }
            else
            {
                Console.WriteLine("The Folder already exists");
            }
            DirectoryInfo dInfo = new DirectoryInfo(file);
            DirectorySecurity dSecurity = dInfo.GetAccessControl();
            dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
            dInfo.SetAccessControl(dSecurity);

        }
Run Code Online (Sandbox Code Playgroud)

上面的代码将文件夹的访问权限设置为对每个用户(每个人)的完全控制/读写.