gro*_*wse 7 .net c# permissions uac
我正在开发一个需要将文件复制到文件系统上给定目录的软件.它需要适用于UAC感知操作系统(Vista,7)以及XP.为了解决写入需要UAC提升的目录的问题,应用程序实际上启动了另一个进程,其中包含一个表明需要UAC的清单.这会生成提示,然后在用户确认时执行复制.
从我所看到的,一个目录可以有三种不同的逻辑权限状态 - 可写,没有UAC提升,可写与UAC提升,不可写.
我的问题是:对于给定目录,如何可靠地确定当前用户是否可以将文件复制(并可能覆盖)到该目录,如果可以,我如何确定是否需要UAC提升?
在XP上,这可能就像检查是否允许"允许写入"权限一样简单,但在Vista/7上,有些目录未授予此权限,但UAC仍然可以执行此操作.
tes*_*ino 11
我们有一个WriteAccess文件的方法,你可以适应它的目录(Directory.GetAccessControl等)
/// <summary> Checks for write access for the given file.
/// </summary>
/// <param name="fileName">The filename.</param>
/// <returns>true, if write access is allowed, otherwise false</returns>
public static bool WriteAccess(string fileName)
{
if ((File.GetAttributes(fileName) & FileAttributes.ReadOnly) != 0)
return false;
// Get the access rules of the specified files (user groups and user names that have access to the file)
var rules = File.GetAccessControl(fileName).GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
// Get the identity of the current user and the groups that the user is in.
var groups = WindowsIdentity.GetCurrent().Groups;
string sidCurrentUser = WindowsIdentity.GetCurrent().User.Value;
// Check if writing to the file is explicitly denied for this user or a group the user is in.
if (rules.OfType<FileSystemAccessRule>().Any(r => (groups.Contains(r.IdentityReference) || r.IdentityReference.Value == sidCurrentUser) && r.AccessControlType == AccessControlType.Deny && (r.FileSystemRights & FileSystemRights.WriteData) == FileSystemRights.WriteData))
return false;
// Check if writing is allowed
return rules.OfType<FileSystemAccessRule>().Any(r => (groups.Contains(r.IdentityReference) || r.IdentityReference.Value == sidCurrentUser) && r.AccessControlType == AccessControlType.Allow && (r.FileSystemRights & FileSystemRights.WriteData) == FileSystemRights.WriteData);
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
9257 次 |
| 最近记录: |