Reputation: 137
I want to be able to have my user get read/write privileges to folders that they don't have access to without them having to do extra work. I'd like for the box to appear below, then they can simply hit "continue", then the program will progress.
I'm able to make the box appear by using
Process.Start(filePath);
but then that also opens the folder, which is a bit clunky. Is there a way to just show this dialog, wait for the user to interact, then continue execution?
Upvotes: 1
Views: 1006
Reputation: 9473
What I will do in this case:
test permission on file, you can add what ever is needed (read, execute, traverse)
public static bool HasWritePermissionOnDir(string path)
{
var writeAllow = false;
var writeDeny = false;
var accessControlList = Directory.GetAccessControl(path);
if (accessControlList == null)
return false;
var accessRules = accessControlList.GetAccessRules(true, true,
typeof(System.Security.Principal.SecurityIdentifier));
if (accessRules ==null)
return false;
foreach (FileSystemAccessRule rule in accessRules)
{
if ((FileSystemRights.Write & rule.FileSystemRights) != FileSystemRights.Write)
continue;
if (rule.AccessControlType == AccessControlType.Allow)
writeAllow = true;
else if (rule.AccessControlType == AccessControlType.Deny)
writeDeny = true;
}
return writeAllow && !writeDeny;
}
then if there is no access
// Add the access control entry to the file.
AddFileSecurity(fileName, @"DomainName\AccountName",
FileSystemRights.ReadData, AccessControlType.Allow);
FileSystemRights - set as needed source
Put all operations in try-catch blocks as dealing with files are always gona to have issues :-)
Ensure that you user has a privilege to change permissions (run as Admin)
Upvotes: 1