Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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
计算asp.net中的文件夹总数_Asp.net - Fatal编程技术网

计算asp.net中的文件夹总数

计算asp.net中的文件夹总数,asp.net,Asp.net,我需要读取一个包含多个内部文件夹的文件夹,其中包含100多个xml文件。我需要逐一读取所有这些xml文件。我正在使用asp.net c#。我怎样才能做到这一点 例如:A是我的文件夹,包含1,2,3,4,5,6…200作为子文件夹。 现在文件夹1包含a.xml,b.xml,c.xml。。。类似地,文件夹2包含1.xml,2.xml,3.xml。。。 现在,我需要从每个文件夹中逐个读取所有这些xml文件。您可以使用并行linq并执行以下操作 int count = 0; string[]

我需要读取一个包含多个内部文件夹的文件夹,其中包含100多个xml文件。我需要逐一读取所有这些xml文件。我正在使用asp.net c#。我怎样才能做到这一点

例如:
A
是我的文件夹,包含
1
2
3
4
5
6
200
作为子文件夹。 现在文件夹
1
包含
a.xml
b.xml
c.xml
。。。类似地,文件夹
2
包含
1.xml
2.xml
3.xml
。。。
现在,我需要从每个文件夹中逐个读取所有这些xml文件。

您可以使用并行linq并执行以下操作

  int count = 0;
    string[] files = null;
    try
    {
        files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
    }
    catch (UnauthorizedAccessException e)
    {
        Console.WriteLine("You do not have permission to access one or more folders in this directory tree.");
        return;
    }

    catch (FileNotFoundException)
    {
        Console.WriteLine("The specified directory {0} was not found.", path);
    }

    var fileContents = from file in files.AsParallel()
            let extension = Path.GetExtension(file)
            where extension == ".xml" 
            let text = File.ReadAllText(file)
            select new FileResult { Text = text , FileName = file }; //Or ReadAllBytes, ReadAllLines, etc.               

    try
    {
        foreach (var item in fileContents)
        {
            Console.WriteLine(Path.GetFileName(item.FileName) + ":" + item.Text.Length);
            count++;
        }
    }
    catch (AggregateException ae)
    {
        ae.Handle((ex) =>
            {
                if (ex is UnauthorizedAccessException)
                {
                   Console.WriteLine(ex.Message);
                   return true;
                }
                return false;
            });
    }

示例摘自:

这对您有用吗???有多少个文件夹级别。如果它是无限的,你需要使用递归