Number of files in a folder

How to get the number of files from a folder using ASP.NET using C #?

+49
Feb 11 2018-10-11T00
source share
7 answers
System.IO.Directory myDir = GetMyDirectoryForTheExample(); int count = myDir.GetFiles().Length; 
+36
Feb 11 2018-10-10T00
source share

You can use the Directory.GetFiles method

Also see Directory.GetFiles method (String, String, SearchOption)

You can specify a search option in this overload.

TopDirectoryOnly : includes only the current directory in the search.

AllDirectories . Includes the current directory and all subdirectories in the search operation. This option includes reprocessing points, such as installed drives and search symlinks.

 // searches the current directory and sub directory int fCount = Directory.GetFiles(path, "*", SearchOption.AllDirectories).Length; // searches the current directory int fCount = Directory.GetFiles(path, "*", SearchOption.TopDirectoryOnly).Length; 
+79
Feb 11 2018-10-11T00
source share

The weakest method is to use LINQ :

 var fileCount = (from file in Directory.EnumerateFiles(@"H:\iPod_Control\Music", "*.mp3", SearchOption.AllDirectories) select file).Count(); 
+17
Sep 15 '11 at 12:39 on
source share
 System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("SourcePath"); int count = dir.GetFiles().Length; 

You can use this.

+8
Aug 08 2018-12-12T00:
source share

Reading PDF files from a directory:

 var list = Directory.GetFiles(@"C:\ScanPDF", "*.pdf"); if (list.Length > 0) { } 
+3
Nov 04 '14 at 5:09
source share

Try the following code to get the number of files in the folder

  string strDocPath = Server.MapPath('Enter your path here'); int docCount = Directory.GetFiles(strDocPath, "*", SearchOption.TopDirectoryOnly).Length; 



0
Oct 13 '17 at 11:30
source share

To get the number of specific type extensions using LINQ , you can use this simple code:

 Dim exts() As String = {".docx", ".ppt", ".pdf"} Dim query = (From f As FileInfo In directory.GetFiles()).Where(Function(f) exts.Contains(f.Extension.ToLower())) Response.Write(query.Count()) 
-one
Jan 20 '17 at 9:07 on
source share



All Articles