简体   繁体   English

C#拒绝访问路径'C:\ Documents and Settings \'

[英]C# Access to the path 'C:\Documents and Settings\' is denied

I'm using a simple DirectoryInfo to grab all directories on the root on the C drive. 我正在使用一个简单的DirectoryInfo来获取C盘上根目录下的所有目录。 However, I'm running under administrator and i'm getting the error of path access denied, below is the code that I am running. 但是,我在管理员下运行,我得到路径访问被拒绝的错误,下面是我正在运行的代码。 How do I resolve the issue of path access? 如何解决路径访问问题?

DirectoryInfo Dinfo = new DirectoryInfo(@"C:\");
DirectoryInfo[] directories = Dinfo.GetDirectories("*.*", SearchOption.AllDirectories);

On newer versions of Windows C:\\Document and Settings is a junction point , kind of a file system shortcut. 在较新版本的Windows C:\\Document and Settings是一个连接点 ,一种文件系统快捷方式。 It is not a normal directory, which means that it doesn't really work as a normal directory. 它不是一个普通的目录,这意味着它并不像普通目录那样真正起作用。

If you type in C:\\Document and Settings in the start->run box you will also get an access denied error, so it is nothing specific to your program. 如果在开始 - >运行框中键入C:\\Document and Settings ,您还将收到拒绝访问错误,因此它不是您的程序特有的。

I'm a bit confused by how this works however. 但是我对它的运作方式感到有些困惑。 I thought that the junction point would be a transparent link to the new location which is c:\\users but obviously not. 我认为交接点是一个透明链接到新位置c:\\users但显然不是。

Edit 编辑

After looking at the duplicate question I'm less confused. 看完这个重复的问题之后我就不那么困惑了。 The junction point really links to the new location which is c:\\users . 连接点确实链接到新位置,即c:\\users However, there is an explicit deny acl for reading on the junction point to prevent anyone from using it to read things: 但是,有一个明确的拒绝acl用于读取连接点以防止任何人使用它来读取内容:

C:>cacls "Documents and Settings" C:\\Documents and Settings Everyone:(DENY)(special access:) C:> cacls“Documents and Settings”C:\\ Documents and Settings Everyone :( DENY)(特殊访问:)

  FILE_READ_DATA Everyone:R NT AUTHORITY\\SYSTEM:F BUILTIN\\Administrators:F 

C:> C:>

If you are running as Administrator you might still run into 如果您以管理员身份运行,您可能仍会遇到

  • UAC being active. UAC活跃。 Fix by using "run as..." 使用“run as ...”进行修复
  • Folders that you can only access when you explicitly take ownership. 您只能在明确获得所有权时才能访问的文件夹。

C:\\ Document and Settings是一个交接点,另外你无法访问系统卷信息目录。它放在C:\\ root上你必须捕获任何安全异常并跳过它来处理你的代码。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace Solution
{
    public class Program
    {
 static void Main(string[] args)
        {
            DirSearch(@"YOUR PATH");
            Console.ReadKey();
        }

        static void DirSearch(string dir)
        {
            try
            {
                foreach (string f in Directory.GetFiles(dir))
                    Console.WriteLine(f);
                foreach (string d in Directory.GetDirectories(dir))
                {
                    Console.WriteLine(d);
                    DirSearch(d);
                }

            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

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

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