2

I want to restrict what folder a person can choose to set their default save path in my app. Is there a class or method which would allow me to check access rights and either limit the user's options or show an error once they have made their selection. Is FileSystemSecurity.AccessRightType a possibility?

Jeff B
  • 8,572
  • 17
  • 61
  • 140
The_Cthulhu_Kid
  • 1,839
  • 1
  • 31
  • 42
  • Would it not be better to let the user choose whatever location they want that they have access to? It is their computer, after all. By all means offer a default that you think is sensible, but let them have the final say. – DavidK Sep 14 '12 at 14:28
  • Is this to prevent an user from picking a folder they don't have access to, but just not showing it at all in the first place? – Jeff B Sep 14 '12 at 14:30
  • @DavidK I have to allow users to set a default save path but the software is meant to be implemented in schools and colleges (like the one I am in) where the students have restricted access to certain folders. – The_Cthulhu_Kid Sep 14 '12 at 14:40
  • @JeffBridgman That would be an absolutely perfect solution but I have no idea how to implement it. Any ideas? – The_Cthulhu_Kid Sep 14 '12 at 14:41

1 Answers1

1

Since the FolderBrowserDialog is a rather closed control (it opens a modal dialog, does it stuff, and lets you know what the user picked), I don't think you're going to have much luck intercepting what the user can select or see. You could always make your own custom control, of course ;)

As for testing if they have access to a folder

private void OnHandlingSomeEvent(object sender, EventArgs e)
{
  DialogResult result = folderBrowserDialog1.ShowDialog();
  if(result == DialogResult.OK)
  {
      String folderPath = folderBrowserDialog1.SelectedPath;
      if (UserHasAccess(folderPath)) 
      {
        // yay! you'd obviously do something for the else part here too...
      }
  }
}

private bool UserHasAccess(String folderPath)
{
  try
  {
    // Attempt to get a list of security permissions from the folder. 
    // This will raise an exception if the path is read only or do not have access to view the permissions. 
    System.Security.AccessControl.DirectorySecurity ds =
      System.IO.Directory.GetAccessControl(folderPath);
    return true;
  }
  catch (UnauthorizedAccessException)
  {
    return false;
  }
}

I should note that the UserHasAccess function stuff was obtained from this other StackOverflow question.

Community
  • 1
  • 1
Jeff B
  • 8,572
  • 17
  • 61
  • 140
  • 1
    Thank you very much. This looks great. I can show a message and reset to default. As to the custom control... maybe in a year or two. – The_Cthulhu_Kid Sep 14 '12 at 14:55
  • This actually solved another problem too. When people want to upload directories or files I can call the same method to see whether or not to allow them. Thanks again. – The_Cthulhu_Kid Sep 14 '12 at 17:28