1

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?

enter image description here

user2961759
  • 137
  • 1
  • 8
  • is this code executed as performing user? do you want to perform this in background? - just set permissions? – profesor79 Mar 07 '16 at 17:54
  • @profesor79 - If I could do it in the background that would be great, still trying to look up how to do it. Thanks. – user2961759 Mar 07 '16 at 18:07

1 Answers1

1

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

source

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)

Community
  • 1
  • 1
profesor79
  • 9,213
  • 3
  • 31
  • 52
  • Hm, I've tried this, and I've also the code at the following thread: http://stackoverflow.com/questions/8944765/c-sharp-set-directory-permissions-for-all-users-in-windows-7 And like that guy, I'm not having any luck. The code completes successfully, but the rights aren't given as expected. – user2961759 Mar 07 '16 at 20:40