简体   繁体   English

为什么我的Documents and Settings文件夹出现拒绝访问错误?

[英]Why am I getting an access denied error for the Documents and Settings folder?

I'm writing a program that gets all directories and sub-directories. 我正在编写一个获取所有目录和子目录的程序。 I'm using the following code: 我正在使用以下代码:

DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
    string[] directories = Directory.GetDirectories(drive.Name, "*", SearchOption.AllDirectories);
}

But I'm getting an exception stating "Access to the path 'C:\\Documents and Settings\\' is denied." 但我得到一个例外,说“访问路径'C:\\ Documents and Settings \\'被拒绝。”

I'm using Windows 7 and I don't see a C:\\Documents and Settings\\ folder in Explorer. 我正在使用Windows 7,我在资源管理器中看不到C:\\ Documents and Settings \\文件夹。 I enabled "Show hidden files and folders" and even try to type in the path directly but it gives the following error: "C:\\Documents and Settings is not accessible. Access denied." 我启用了“显示隐藏文件和文件夹”,甚至尝试直接键入路径,但它出现以下错误:“C:\\ Documents and Settings无法访问。访问被拒绝。”

Why is Directory.GetDirectories() pulling a directory that doesn't seem to exist? 为什么Directory.GetDirectories()拉出一个似乎不存在的目录?

This directory is what is known as a junction point , which should be pointing to c:\\users. 这个目录就是所谓的连接点 ,它应该指向c:\\ users。

From the MSDN documentation: 从MSDN文档:

These junction points can be identified as follows: 这些交接点可以识别如下:

They have the FILE_ATTRIBUTE_REPARSE_POINT, FILE_ATTRIBUTE_HIDDEN, and FILE_ATTRIBUTE_SYSTEM file attributes set. 它们具有FILE_ATTRIBUTE_REPARSE_POINT,FILE_ATTRIBUTE_HIDDEN和FILE_ATTRIBUTE_SYSTEM文件属性集。

They also have their access control lists (ACLs) set to deny read access to everyone. 他们还将访问控制列表(ACL)设置为拒绝对每个人的读取访问权限。

Applications that call out a specific path can traverse these junction points if they have the required permissions. 如果具有所需权限,则调用特定路径的应用程序可以遍历这些连接点。 However, attempts to enumerate the contents of the junction points will result in failures. 但是,枚举连接点内容的尝试将导致失败。

After looking for an answer for some time - I decided to write the code on my own. 在寻找答案一段时间之后 - 我决定自己编写代码。

I'm sharing here the basic idea and not the full code - Take the important part and use it implement it in your code. 我在这里分享基本的想法,而不是完整的代码 - 采取重要的部分,并使用它在您的代码中实现它。

Worked for me. 为我工作。

public void directoryCrawl(string startFolder)
    {

    try
    {
        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder);

        /* here you can add "dir" to some kind of list of your choice. */

        foreach (System.IO.DirectoryInfo directory in dir.GetDirectories())
        {
            try
            {
                directoryCrawl(directory.FullName);
            }
            catch
            {
                Console.Writeline("Access denied to: \"" + directory.FullName + "\".");
            }
        }
    }
    catch
    {
        if (!String.IsNullOrEmpty(startFolder))
        {
            Console.Writeline("Access denied to: \"" + startFolder + "\".");
        }
        }
        return;
    }

I don't know how to work around, but I can tell you that WinXP used that path. 我不知道如何解决,但我可以告诉你,WinXP使用了这条路径。 Old programs written with an expectation of being able to access that folder would not have been Win7 compatible so Microsoft redirects it to your Users folder. 期望能够访问该文件夹的旧程序不会与Win7兼容,因此Microsoft将其重定向到您的Users文件夹。

If I do Start -> Run c:\\Documents and Settings I also get the Access is Denied error. 如果我执行开始 - >运行c:\\Documents and Settings我也会收到Access is Denied错误。 So something is there. 所以有些东西存在。

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

相关问题 如何访问“文档和设置”文件夹? - How can I access the "Documents and Settings" folder? 使用 Microsoft Graph 成功进行交互式身份验证后,为什么会出现错误访问被拒绝? - Why am I getting Error Access Denied after successful interactive authentication with Microsoft Graph? 为什么我在 c# 中收到 COM 端口访问被拒绝 - Why I am getting COM port access denied in c# 为什么我试图制作StreamWriter实例时拒绝文件访问? - Why am I getting file access denied trying to make a StreamWriter instance? 当我在创建的文件夹中写入文件时,总是出现访问被拒绝错误 - Always Getting Access Denied Error When I Write A File Inside Created Folder 为什么我在此文件夹上收到UnauthorizedAccessException? - Why am I getting UnauthorizedAccessException on this folder? 访问被拒绝写入文件夹,为什么? - Access denied to write to folder, Why? C#,是否可以在Vista中访问文档和设置? - C#, getting access to Documents and Settings in Vista? C#拒绝访问路径'C:\ Documents and Settings \' - C# Access to the path 'C:\Documents and Settings\' is denied 提升C#中的权限,拒绝访问路径'c:\\ Documents and Settings' - elevate privileges in C# , Access to the path 'c:\Documents and Settings' is denied
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM