简体   繁体   English

如何检查给定目录的访问权限?

[英]How can I check access rights on a given directory?

Currently I have a program that searches a user set directory and sub-directories for music files and adds them to a collection. 目前,我有一个程序可以在用户设置的目录和子目录中搜索音乐文件,并将它们添加到集合中。 However if one of the directories it comes accross is protected then the program falls over. 但是,如果其中的目录之一受到保护,则程序将崩溃。 I wanted to know how I can check if the user has access to the directory before trying to search it to avoid this problem. 我想知道如何在尝试搜索目录之前检查用户是否有权访问目录,以避免出现此问题。 Below is the code I'm using for the search, it currently contains a basic work around for "System Volume Information" but as there is a possibility that there may be other protected directories I wanted to change this to include them. 下面是我用于搜索的代码,它当前包含有关“系统卷信息”的基本解决方法,但是由于可能存在其他受保护的目录,因此我想对其进行更改以包括它们。

    public void SearchForMusic()
    {
        //Searches selected directory and its sub directories for music files and adds their path to ObservableCollection<string> MusicFound
        foreach (string ext in extentions)
        {
            foreach (string song in Directory.GetFiles(SearchDirectory, ext))
            {
                musicFound.Add(song);                   
            }

            foreach (string directory in Directory.GetDirectories(SearchDirectory))
            {
                if (directory.Contains("System Volume Information"))
                {

                }
                else
                {
                    foreach (string song in Directory.GetFiles(directory, ext))
                    {
                    musicFound.Add(song);
                    }

                    foreach (string subDirectory in Directory.GetDirectories(directory))
                    {
                        foreach (string subSong in Directory.GetFiles(subDirectory, ext))
                        {
                            musicFound.Add(subSong);
                        }
                    }
                }
            }
        }
    }

Many thanks :) 非常感谢 :)

By far the easiest way to be sure that you have access to a file system object is to attempt to access it. 到目前为止,确保您有权访问文件系统对象的最简单方法是尝试访问它。 If it fails with an Access Denied error, then you don't have access. 如果失败并显示“访问被拒绝”错误,则说明您无权访问。 Just detect that error condition and proceed with the next item in the search. 只需检测该错误情况,然后继续搜索中的下一项即可。

In other words, delegate checking access to the system which is, after all, the ultimate arbiter of access rights. 换句话说,委托检查对系统的访问,毕竟这是访问权限的最终仲裁者。

You can check this question by replacing the Write with Read permissions. 您可以通过将“写入”替换为“读取”权限来检查问题。 Also, wrap your code in a try catch block and if the exception is thrown, you can assume (or properly check the exception type to be sure) that the directory cannot be traversed. 另外,将代码包装在try catch块中,如果引发了异常,则可以假定(或正确检查异常类型以确保)无法遍历该目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM