【问题标题】:UnauthorizedAccessException, how to ignore and keep program running?UnauthorizedAccessException,如何忽略并保持程序运行?
【发布时间】:2018-05-03 23:51:00
【问题描述】:

每次我运行下面的代码时,我都会得到一个UnauthorizedAccessException。我添加了 try 和 catch 块,这防止了错误,但它阻止了程序死在它的轨道上。

有没有办法可以忽略此错误并读取未经授权的访问文件?如果没有,我只想让我的程序跳过这些文件继续而不停止。

代码如下:

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

namespace Interface
{
    class Progra
    {
        static void Main(string[] args)
        {
            try
            {
                int number = 0;
                string[] files = Directory.GetFiles("C:\\", "*.*", 
                    SearchOption.AllDirectories);

                foreach (string file in files)
                {
                    number = number + 1;

                    Console.ForegroundColor = ConsoleColor.Green;
                    DateTime now = DateTime.Now;
                    Console.Write("[" + now + "]");

                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.Write(" [" + number + "] ");

                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write(file + "\n");
                }

                Console.WriteLine("");
                Console.WriteLine("- " + number + " files found!");
                Console.ReadKey();
            }
            catch
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("- An unknown error occoured and the contents " + 
                    "of this folder can not be displayed.\n");
                Console.ForegroundColor = ConsoleColor.White;
            }

            Console.ReadKey();
        }
    }
}

如果有答案,你能告诉我我该怎么做吗? C# 的菜鸟:P。

【问题讨论】:

标签: c#


【解决方案1】:
try
{
    int number = 0;
    string[] files = Directory.GetFiles("C:\\", "*.*",
        SearchOption.TopDirectoryOnly);

    foreach (string file in files)
    {
        try
            {
                string[] innerFiles = Directory.GetFiles(file, "*.*",
                    SearchOption.TopDirectoryOnly);
            }
            catch (Exception e)
            {
                // stuff here
            }

            number = number + 1;

            Console.ForegroundColor = ConsoleColor.Green;
            DateTime now = DateTime.Now;
                Console.Write("[" + now + "]");

                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.Write(" [" + number + "] ");

                Console.ForegroundColor = ConsoleColor.White;
                Console.Write(file + "\n");
            }

            Console.WriteLine("");
            Console.WriteLine("- " + number + " files found!");
            Console.ReadKey();
        }
        catch (Exception e)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("- An unknown error occoured and the contents " +
                "of this folder can not be displayed.\n");
            Console.ForegroundColor = ConsoleColor.White;
        }

        Console.ReadKey();
    }

这是大量的工作,但我有点让你开始了。字符串数组的声明发生了异常,一旦发生这种情况,它就永远不会进入 foreach 循环并刚刚结束。如果您希望它在失败后继续循环,将异常移动到循环内部,这样就会发生捕获并返回循环。您需要做的另一件事是使其递归,循环遍历文件结构中的每一级,而不是一次抓取所有文件。老实说,从 Microsoft 获取文件的方法不是很好……它可能会为您解决这个问题,但我敢肯定它没有这样做是有原因的。

我不确定该程序的用途,但您可能需要在 foreach 循环中的某个时间点区分文件和目录。

【讨论】:

  • 只是统计和列出系统中的文件数。就是图个好玩儿。 :)
  • 如果你正在测试,不要试图列出你不熟悉的director中的所有文件。对我来说,如果我想Directory.GetFiles,我会在我创建的目录中进行测试。当然,我会知道预期的输出是什么。如果您在任何其他目录中尝试,您可能不知道预期的结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-09
  • 2017-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多