Csharp/C#教程:如何在使用.NET安装期间为文件夹提供读/写权限分享


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

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

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

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

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

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

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

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

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

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

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

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

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

要做的是做一个自定义动作。 这非常简单,请查看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); } 

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

 /folder="[CommonAppDataFolder][ProductName]" 

 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); } 

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

行为是设计的。 程序不应该自己修改(因此它们的安装目录)除了更新之外的任何东西(再次可以使用Windows安装程序完成而没有问题)。 如果您使用的是.NET, 隔离存储是存储用户数据的绝佳位置。

 DirectoryInfo info = new DirectoryInfo(path[x]); DirectorySecurity security = info.GetAccessControl(); security.AddAccessRule(new FileSystemAccessRule(logonName, FileSystemRights.Modify, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow)); security.AddAccessRule(new FileSystemAccessRule(logonName, FileSystemRights.Modify, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)); info.SetAccessControl(security); 

如果要保存和访问ProgramData文件夹中的多个文件,设置inheritance部分也很重要。

如上所述,Users组在Program Files中没有写权限。 如果您不想处理安装程序类或Wix(如果它是一个简单的程序),只需更喜欢在Windows Volume下安装您的软件。

我在谈论Visual Studio安装向导 :在目标机器上的文件系统中将应用程序文件夹’ DefaultLocation ‘属性从[ProgramFilesFolder]更改为[WindowsVolume] [制造商] [ProductName]。

上述就是C#学习教程:如何在使用.NET安装期间为文件夹提供读/写权限分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/984540.html

(0)
上一篇 2021年12月20日
下一篇 2021年12月20日

精彩推荐