Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.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/1/asp.net/35.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#_Asp.net - Fatal编程技术网

C# 目录中的文件夹数

C# 目录中的文件夹数,c#,asp.net,C#,Asp.net,如何知道目录中的文件夹数 我尝试使用System.IO.Directory,但没有成功 使用: Directory.GetDirectories(@"C:\").Length 当然,您不必使用@“C:\”而是使用您想要知道其子目录计数的任何路径。该方法还具有重载,允许搜索特定模式和递归搜索。您有两个选项: int directoryCount = System.IO.Directory.GetDirectories(@"c:\yourpath\").Length 或 如果您需要对它们执行其他

如何知道目录中的文件夹数

我尝试使用
System.IO.Directory
,但没有成功

使用:

Directory.GetDirectories(@"C:\").Length

当然,您不必使用
@“C:\”
而是使用您想要知道其子目录计数的任何路径。该方法还具有重载,允许搜索特定模式和递归搜索。

您有两个选项:

int directoryCount = System.IO.Directory.GetDirectories(@"c:\yourpath\").Length

如果您需要对它们执行其他操作,并且正在使用.NET 4,那么出于性能原因,您也可以使用DirectoryInfo.EnumerateDirectories()函数


是的,有很多选择。如果您仍然有问题,您可能希望让我们知道在使用System.IO.Directory时哪些不起作用。

要计算文件夹中的文件数:-

string[] My_file = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
MessageBox.Show("Files Found: " + My_file.Length.ToString());
要计算目录中的文件夹数,请执行以下操作:-

MessageBox.Show("Folder Count:" + Directory.GetDirectories(folderBrowserDialog1.SelectedPath).Length.ToString(), "Message");

EnumerateDirectories(…).Count()
如果子目录的数量很大,可能会减少内存消耗。@svick:谢谢,我不知道这件事。在我的工作场所,仍然停留在3.5上,因此一些不太突出的4.0特性在我身上消失了:)
MessageBox.Show("Folder Count:" + Directory.GetDirectories(folderBrowserDialog1.SelectedPath).Length.ToString(), "Message");