6

我正在使用一个简单DirectoryInfo的方法来抓取 C 驱动器根目录上的所有目录。但是,我在管理员下运行并且我收到路径访问被拒绝的错误,下面是我正在运行的代码。如何解决路径访问问题?

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

4 回答 4

4

On newer versions of Windows C:\Document and Settings is a junction point, kind of a file system shortcut. 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.

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.

Edit

After looking at the duplicate question I'm less confused. The junction point really links to the new location which is c:\users. However, there is an explicit deny acl for reading on the junction point to prevent anyone from using it to read things:

C:>cacls "Documents and Settings" C:\Documents and Settings Everyone:(DENY)(special access:)

                               FILE_READ_DATA

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

C:>

于 2012-06-09T19:27:09.457 回答
1

如果您以管理员身份运行,您可能仍会遇到

  • UAC 处于活动状态。使用“运行方式...”修复
  • 只有当您明确获得所有权时才能访问的文件夹。
于 2012-06-09T19:29:02.487 回答
1

C:\Document and Settings 是一个连接点,此外您无法访问系统卷信息目录。它位于 C:\ 根目录下,您必须捕获任何安全异常并跳过它以运行您的代码。

于 2012-06-09T19:31:30.003 回答
-1
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);
            }
        }
    }
}
于 2015-09-24T06:55:21.550 回答