【问题标题】:C# cannot search for .txt files on c:\ drive [duplicate]C# 无法在 c:\ 驱动器上搜索 .txt 文件 [重复]
【发布时间】:2016-12-27 20:38:29
【问题描述】:

我想显示我系统上的所有 .txt 文件。但是当我执行我的代码时,我只会在我的可移动磁盘驱动器(USB-Stick)上显示 .txt 文件。

我该如何解决这个问题?

感谢您的帮助:D

这是我的代码:

static List<string> Search()
{
    var files = new List<string>();

    foreach (DriveInfo d in DriveInfo.GetDrives().Where(x => x.IsReady))
    {
        try
        {
            files.AddRange(Directory.GetFiles(d.RootDirectory.FullName, "*.txt", SearchOption.AllDirectories));
            Console.Write(string.Join(System.Environment.NewLine, files));
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

    return files;
}

我的控制台输出:

The access to the path "C: \$ Recycling. Bin\S-1-5-18" was refused.
D:\Test\Test1.txt
D:\Test\Test2.txt
...
D:\PortableApps\EclipsePortable\Other\Source\Readme.txtSystem.Collections.Generic.List`1[System.String]

我根本没有收到任何错误消息。

【问题讨论】:

  • 你有什么错误吗?如果是这样,请发布它的详细信息。
  • 对不起,我应该发布我的控制台输出^^要编辑我的帖子
  • 我得到了同样的错误。以管理员身份运行通过,但随后在 c:\documents 和设置上出错。最好跳过这些目录。
  • “我根本没有收到任何错误消息”是什么意思?第一行是一条错误消息。有东西捕捉到了异常并显示了一条错误消息。
  • 我不认为这是一条错误消息,因为它只是告诉我我没有该目录的权限。

标签: c# .net


【解决方案1】:

由于安全原因,GetFiles 函数似乎无法访问某些文件系统。请参考这个解决方案: Access to all file in C drive

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    相关资源
    最近更新 更多