【问题标题】:Set default access permissions on a folder created in a custom installer对在自定义安装程序中创建的文件夹设置默认访问权限
【发布时间】:2012-01-04 16:38:47
【问题描述】:

我创建了一个简单的自定义安装程序,如 here 所述。我的新安装程序按预期工作。它会创建一个文件夹并将路径添加到 app.config 文件中。

问题是文件夹上的只读标志被设置并且非管理员用户的写权限被删除。

如果我手动创建文件夹并设置权限,它们会在安装程序运行时重置。

如何在我的自定义安装程序中指定这些参数?

编辑:

我使用下面显示的代码为每个人设置安全权限,这工作正常。

// Get the directory info for the existing folder
DirectoryInfo dirInfo = new DirectoryInfo(settingsFileDir);

// Now apply user access permissions to the folder
if (dirInfo != null)
{
    DirectorySecurity security = dirInfo.GetAccessControl();
    security.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
    security.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
    dirInfo.SetAccessControl(security);
}

当我尝试设置文件夹属性以删除只读设置时,没有进行任何更改。我这样做:

dirInfo.Attributes = FileAttributes.Normal;

【问题讨论】:

    标签: windows-installer custom-action


    【解决方案1】:

    使用下面的代码确实设置了文件夹的访问权限。

    // Get the directory info for the existing folder
    DirectoryInfo dirInfo = new DirectoryInfo(settingsFileDir);
    
    // Now apply user access permissions to the folder
    if (dirInfo != null)
    {
        DirectorySecurity security = dirInfo.GetAccessControl();
        security.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
        security.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
        dirInfo.SetAccessControl(security);
    }
    

    如果您查看文件夹的属性,您可以看到 readonly 属性似乎已设置,但确实允许写入文件。

    【讨论】:

      【解决方案2】:

      要修改权限,您可以尝试将XCACLS.EXE 用作custom action

      要更改只读属性,您可以使用带有自定义代码的自定义操作。这是一篇可能会有所帮助的文章:http://blogs.technet.com/b/heyscriptingguy/archive/2004/10/19/how-can-i-change-a-read-only-file-to-a-read-write-file.aspx

      【讨论】:

      • 我不确定使用外部应用程序是否是最好的方法。它需要它与我的应用程序一起提供,它似乎可能不适用于较新的操作系统。
      • 您使用什么工具来创建 MSI?
      • Visual Studio 2008。我创建了一个自定义操作安装程序类,用于将文件路径应用到 app.config 文件中,然后将任何现有文件移动到此文件夹。
      • Visual Studio 安装项目不支持文件夹权限。因此,您需要自定义操作或其他设置创作工具。此外,在安装过程中移动已安装的文件也不是一个好主意。
      猜你喜欢
      • 2017-04-04
      • 1970-01-01
      • 2014-06-05
      • 2019-08-02
      • 1970-01-01
      • 2017-03-15
      • 2017-09-03
      • 2021-12-17
      • 2010-12-10
      相关资源
      最近更新 更多