System.UnauthorizedAccessException的获取目录时 [英] System.UnauthorizedAccessException when getting directories

查看:109
本文介绍了System.UnauthorizedAccessException的获取目录时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很新的C#,所以我一直工作在一个小宠物的项目。

I am really new to C#, so I've been working on a small pet project.

我创建了一个小程序,比较一个目录的大小给定大小。如果该目录是相等或更大,那么它记录的路径目录。

I've created a small program that compares the size of a directory with a given size. And if the directory is equal or larger, then it logs the path to that directory.

long size = Convert.ToInt32(Size) * 1024 * 1024;
string[] directories = Directory.GetDirectories(path, "*", SearchOption.AllDirectories); //the error occurs on this line
Array.Sort(directories);

foreach (string name in directories)
   try
   {
      DirectoryInfo directory = new DirectoryInfo(name);
      long dir = directory.EnumerateFiles("*", SearchOption.AllDirectories).Sum(fi => fi.Length);

   if (dir >= ScanSize)
      Console.WriteLine(directory);
   }

   catch (UnauthorizedAccessException) {  }

我应该注意的是,输入的字符串,而长尺寸= Convert.ToInt32(尺寸)来自于主要的参数()

我曾经读过我不应该使用

I've read somewhere that I shouldn't use

Directory.GetDirectories(ScanPath,*,SearchOption.AllDirectories);

,因为它会得到所有的目录一次。但如果我删除它,它只是变得给定路径的目录,没有任何子目录。所以,我被告知申请递归,但我发现这相当困难。我读了一些东西 file.Attributes ,有关隐藏文件,但我不知道在哪里可以应用它们。

since it'll get all the directories at once. But if I remove it, it only gets the directories in the given path, without any subdirectories. So I was told to apply recursion, but I found these fairly difficult. I read some things on file.Attributes, about hidden files, but I didn't know where to apply them.

我是系统管理员,我打算到整个数据驱动器上运行此。 D:\

I am the administrator of the system and I plan to run this on the entire data drive. D:\

但是,在这种情况下,当程序试图访问D的垃圾桶发生错误:\但即使跳过这个特定的位置,误差还是回来了另一个无法访问一个

But in this case the error occurs when the program tries to access the trash can of the D:\ But even if it skips this specific location, the error still comes back at another inaccessible one.

我希望任何人知道一个很好的例子,或知道一个网站,对此做出解释。

I'm hoping anyone here knows a good example or knows a website that explains this.

推荐答案

递归是在这里你的朋友。根据需要在回路中加入错误处理。试试这个:

Recursion is your friend here. Add error handling as needed in the loop. Try this:

    private static long maxSize = 5 * 1024 * 1024;

    static void Main(string[] args)
    {

        GetDirectorySize(new DirectoryInfo(@"d:\"));

    }

    static long GetDirectorySize(DirectoryInfo dir)
    {

        long size = 0;

        foreach(DirectoryInfo d in dir.EnumerateDirectories("*",SearchOption.TopDirectoryOnly)) {
            size += GetDirectorySize(d);
        }

        size += dir.EnumerateFiles("*",SearchOption.TopDirectoryOnly).Sum(fi => fi.Length);

        if (size > maxSize)
        {
            Console.WriteLine("Directory: {0} Size: {1}", dir, size);
        }

        return size;
    }

这篇关于System.UnauthorizedAccessException的获取目录时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆