none
遍历文件夹时,如何跳过拒绝访问的文件夹,继续扫描其他文件夹 RRS feed

  • 问题

  • 本人系统是win7。

    我用directoryinfo dirs=directory.getDirectory();获取目录下的所有文件夹,然后用递归继续访问每个文件夹下的所有文件夹。

    但是当访问到一些文件夹拒绝访问时(例如System Volume Information),遍历停止。

    如何让该遍历继续下去?

    2010年5月7日 12:12

答案

  • 处理 UnauthorizedAccessException 异常   

     class Program
        {
            static void Main(string[] args)
            {
                foreach (string d in GetAuthorizedDirectories("C:\\"))
                {
                    Console.WriteLine(d);
                    foreach (string d2 in GetAuthorizedDirectories(d))
                    {
                        Console.WriteLine(d2);
                    }
                }
                Console.ReadKey();
            }

            public static string[] GetAuthorizedDirectories(string path)
            {
                try
                {
                   return Directory.GetDirectories(path);
                }
                catch (UnauthorizedAccessException)
                {
                    return new string[0];
                }
            }
        }


    问题要简单,错误须详细@错误/异常/堆栈信息+操作系统+软件版本+all the context of the issue Hope Helpful | http://www.leoworks.net
    2010年5月8日 9:08

全部回复

  •   DirectoryInfo dirC = new DirectoryInfo("c:\\");
                DirectoryInfo[] dirs = dirC.GetDirectories();
                foreach (DirectoryInfo dir in dirs)
                {
                    MessageBox.Show(dir.Name);
                }

     

    2010年5月7日 12:31
  • 遍历文件和文件夹的方式我已经知道

    问题是,在遍历文件夹遍历到"C:\System Volume Information"的时候,就会弹出拒绝访问的对话框,然后停止遍历。

    我是想让他遇到拒绝访问后,继续遍历下去。

    2010年5月7日 12:49
  • 你的代码为什么在拒绝访问的时候终止呢?不会是忘记处理异常了吧?

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    2010年5月7日 21:13
    版主
  • 处理 UnauthorizedAccessException 异常   

     class Program
        {
            static void Main(string[] args)
            {
                foreach (string d in GetAuthorizedDirectories("C:\\"))
                {
                    Console.WriteLine(d);
                    foreach (string d2 in GetAuthorizedDirectories(d))
                    {
                        Console.WriteLine(d2);
                    }
                }
                Console.ReadKey();
            }

            public static string[] GetAuthorizedDirectories(string path)
            {
                try
                {
                   return Directory.GetDirectories(path);
                }
                catch (UnauthorizedAccessException)
                {
                    return new string[0];
                }
            }
        }


    问题要简单,错误须详细@错误/异常/堆栈信息+操作系统+软件版本+all the context of the issue Hope Helpful | http://www.leoworks.net
    2010年5月8日 9:08
  • 处理 UnauthorizedAccessException 异常   

     class Program
        {
            static void Main(string[] args)
            {
                foreach (string d in GetAuthorizedDirectories("C:\\"))
                {
                    Console.WriteLine(d);
                    foreach (string d2 in GetAuthorizedDirectories(d))
                    {
                        Console.WriteLine(d2);
                    }
                }
                Console.ReadKey();
            }

            public static string[] GetAuthorizedDirectories(string path)
            {
                try
                {
                   return Directory.GetDirectories(path);
                }
                catch (UnauthorizedAccessException)
                {
                    return new string[0];
                }
            }
        }


    问题要简单,错误须详细@错误/异常/堆栈信息+操作系统+软件版本+all the context of the issue Hope Helpful | http://www.leoworks.net


    主要是给了我一个try...catch...的思路,使用UnauthorizedAccessException还是不能解决问题。

    我try...catch...是这样用的。

    directoryinfo[] dirs;

    try

    {

             dirs=directory.getDirectories();

    }

    catch

    {

            dirs=null;

    ]

    if(dirs!=null)

    {

            //继续获取当前文件夹下的目录。

    }

    2010年5月10日 12:06