Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法获取驱动器子文件夹中存在的文本文件_C# - Fatal编程技术网

C# 无法获取驱动器子文件夹中存在的文本文件

C# 无法获取驱动器子文件夹中存在的文本文件,c#,C#,在我的项目中,当我试图获取驱动器中所有文本文件的列表时,我只获取驱动器中直接存在的文件。我无法获取驱动器文件夹中的文本文件,但希望获取。有什么想法吗 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; namespace listofdi

在我的项目中,当我试图获取驱动器中所有文本文件的列表时,我只获取驱动器中直接存在的文件。我无法获取驱动器文件夹中的文本文件,但希望获取。有什么想法吗

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace listofdirectories
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        string str;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["drive"] != null)
            {
               str=Request.QueryString["drive"];
            }

            DirectoryInfo dinfo = new DirectoryInfo(str);
            FileInfo[] Files = dinfo.GetFiles("*.txt");
            foreach (FileInfo file in Files)
            {
                ListBox1.Items.Add(file.Name);
            }

        }
    }
}
也使用接收。如果将其设置为AllDirectory,它将搜索子文件夹

因此,您的代码应该如下所示:

Directory.GetFiles(str, "*.txt", SearchOption.AllDirectories);

检查DirectoryInfo.GetFiles的所有重载。请参阅MSDN上的。未经授权的数据访问异常正在发生如果我尝试此操作,如果您无权访问子文件夹,您希望如何搜索它们?