c# - 为我的程序文件夹的所有用户设置写权限

标签 c# .net permissions windows-xp setup-project

我已经在 Visual Studio 中构建了一个程序。该程序创建一个日志文件并在程序运行时写入其中。因此,我构建了一个安装程序(安装项目),无论哪个用户使用该程序,它都应该为我的程序文件夹设置写入权限。 目前它看起来像这样:

// ...
}
  InitializeComponent();

  string folder = Directory.GetCurrentDirectory();

  DirectorySecurity ds = Directory.GetAccessControl(folder);
  ds.AddAccessRule(new FileSystemAccessRule("Everyone",   //Everyone is important
                                                  //because rights for all users!
   FileSystemRights.Read | FileSystemRights.Write, AccessControlType.Allow));
}
// ...

在最后两行中,我得到一个 System.SystemException:“Die Vertrauensstellung zwischen der primären Domäne und der vertrauenswürdigen Domäne konnte nicht hergestellt werden。”

[翻译:“无法建立主域和受信域之间的信任关系。”]

堆栈跟踪是这样的:

bei System.Security.Principal.NTAccount.TranslateToSids(IdentityReferenceCollection sourceAccounts, Boolean& someFailed)
bei System.Security.Principal.NTAccount.Translate(IdentityReferenceCollection sourceAccounts, Type targetType, Boolean& someFailed)
bei System.Security.Principal.NTAccount.Translate(IdentityReferenceCollection sourceAccounts, Type targetType, Boolean forceSuccess)
bei System.Security.Principal.NTAccount.Translate(Type targetType)
bei System.Security.AccessControl.CommonObjectSecurity.ModifyAccess(AccessControlModification modification, AccessRule rule, Boolean& modified)
bei System.Security.AccessControl.CommonObjectSecurity.AddAccessRule(AccessRule rule)
bei System.Security.AccessControl.FileSystemSecurity.AddAccessRule(FileSystemAccessRule rule)

你知道我能做什么吗? 谢谢

最佳答案

也许最好的答案不是您所要求的。不写入程序文件目录是有充分理由的。尤其是日志数据是暂时的,不应该写在这里。

将日志数据写入 TEMP 环境变量指定的目录是一个更好的主意。如果你这样做,你会为你的用户省去一些麻烦,并防止他们在未来诅咒你的软件。请查看涵盖同一主题的此答案:

Allow access permission to write in Program Files of Windows 7

关于c# - 为我的程序文件夹的所有用户设置写权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3922064/

相关文章:

c# - 如何在单元测试中从 Automapper 模拟 IValueResolver 的依赖关系

c# - 有什么方法可以将此查询重写为更多 "LINQ-ier"吗?

C# - 如果用户在插入记录后刷新页面,则会创建重复记录。我怎样才能防止这种情况发生?

ios - 检测何时询问并授予iOS通知权限?

java - 如何在Intellij IDEA 15中用Tomcat8运行 "hello world"JSP页面?

c# - Windows Phone 通用应用程序在导航时抛出 AccessViolation

c# - 带有实例计数器的基类

c# - 在 Entity Framework 核心中执行业务规则

c# - 对循环缓冲区进行排序的最有效方法

file-io - lua中的权限问题