计算asp.net中的文件夹总数
我需要读取一个包含多个内部文件夹的文件夹,其中包含100多个xml文件。我需要逐一读取所有这些xml文件。我正在使用asp.net c#。我怎样才能做到这一点 例如:计算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[]
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;
});
}
示例摘自:这对您有用吗???有多少个文件夹级别。如果它是无限的,你需要使用递归