Previously wrote a directory through the calculation of all files and subdirectories to obtain the number of files in a directory, the result found that the speed is very slow, far less than the system itself in the directory properties of the speed shown inside.
int filenum = 0;
/**////<summary>
Gets the number of all files in a directory, including files under subdirectories
</summary>
<param name= "Srcpath" ></param>
<returns></returns>
public int Getfilenum (string srcpath)
{
Try
{
Gets a list of files in the source directory that contains an array of files and directory paths
string[] filelist = System.IO.Directory.GetFileSystemEntries (Srcpath);
Iterate through all the files and directories
foreach (string file in FileList)
{
First as directory processing if there is this directory, recall Getfilenum (string srcpath)
if (System.IO.Directory.Exists (file))
Getfilenum (file);
Else
filenum++;
}
}
catch (Exception e)
{
MessageBox.Show (E.tostring ());
}
return filenum;
}
Recently found a more efficient way, just traverse the directory to get the number of files, now share
System.IO.DirectoryInfo dirinfo = new System.IO.DirectoryInfo (Dirpath);
public static int Getfilescount (System.IO.DirectoryInfo dirinfo)
{
int totalfile = 0;
Totalfile + = Dirinfo.getfiles (). Length;
foreach (System.IO.DirectoryInfo subdir in Dirinfo.getdirectories ())
{
Totalfile + + getfilescount (subdir);
}
return totalfile;
}