限制访问某些文件夹用的FolderBrowserDialog

限制访问某些文件夹用的FolderBrowserDialog

本文介绍了限制访问某些文件夹用的FolderBrowserDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要限制哪些文件夹一个人可以选择将他们的默认保存路径在我的应用程序。是否有一个类或方法,这样可以让我检查的访问权限,要么限制了用户的选择,或显示错误,一旦他们做出他们的选择。是FileSystemSecurity.AccessRightType一种可能性?

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?

推荐答案

由于的FolderBrowserDialog 是一个相当封闭的控制(它会打开一个模式对话框,做它的东西,让你知道什么是用户选取),我不认为你有多少运气拦截什么样的用户可以选择或看到。你总是可以让自己的自定义控制,当然;)

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

我应该注意的是, UserHasAccess 函数的东西就是从这个其他的StackOverflow 获得问题

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

这篇关于限制访问某些文件夹用的FolderBrowserDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 09:21