boost :: filesystem :: recursive_directory_iterator在C:\ Users

时间:2018-01-01 16:02:10

标签: c++ boost

我正在扫描整个C驱动器以找到jpg文件,使用以下代码:

std::string extensions[] = { ".jpg" };

recursive_directory_iterator dir("C:\\"), end;
        while (dir != end)
        {



            if (dir->path() == "C:\\Windows" || dir->path() == "C:\\$Recycle.Bin" || dir->path() == "C:\\Program Files" || dir->path() == "C:\\build_static" || dir->path() == "C:\\Perl" || dir->path() == "C:\\Python" || dir->path() == "C:\\Python27" || dir->path() == "C:\\Qt" || dir->path() == "C:\\Qt-5.9.1" || dir->path() == "C:\\boost_1_65_1") //don't recurse in these directories
            {
                dir.no_push();// don't recurse into this directory.
            }

            else
            {

                auto it = std::find_if(begin(extensions), std::end(extensions),
                    [&](const std::string& s)
                {return dir->path().string().find(s) != std::string::npos; });
                if (it != std::end(extensions))
                {

                    cout << dir->path() << endl;


                }

            }
            try
            {
                ++dir;
            }
            catch (boost::filesystem::filesystem_error& fex)
            {
                std::cout << fex.what() << std::endl; //display the error
                cout << dir->path() << endl; //and where it gets stuck
                dir.no_push();
                ++dir;

            }
        }
    }

问题是,它几乎在磁盘上的每个文件夹上工作,但它在C:\ Users文件夹上抛出拒绝访问错误,但在C:\ Program Files(x86)上没有 (代码在没有管理员权限的情况下运行)。

所以它会递归到程序文件(需要管理员权限)而不是C:\ Users(不需要管理员权限)?为什么程序不能访问Users文件夹及其子目录?

几个月前在Windows 7(home)上运行的代码。我在windows 10 pro上运行它并且我得到了这些错误...操作系统是否是原因? P.S:我正在使用boost 1.66(同样尝试1.65,同样的问题)

1 个答案:

答案 0 :(得分:2)

问题确实如下所述:您没有权限阅读/遍历C:\Users

  

所以它会递归到程序文件中(需要管理员)

谁说需要管理员权限?! ,是的,不是阅读遍历

  

而不是C:\ Users(不需要管理员权限)?

完全相反(您拥有对您拥有的部件的写权限,例如您自己的配置文件)

  

为什么程序无法访问Users文件夹及其子目录?

由于操作系统设置了访问控制

我建议阅读NTFS ACL:http://www.ntfs.com/ntfs-permissions-acl-use.htm

相关问题