File count from a folder

前端 未结 10 2384
天命终不由人
天命终不由人 2020-11-27 03:47

How do I get number of Files from a folder using ASP.NET with C#?

相关标签:
10条回答
  • 2020-11-27 03:57

    Try following code to get count of files in the folder

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

    0 讨论(0)
  • 2020-11-27 03:59
    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("SourcePath");
    int count = dir.GetFiles().Length;
    

    You can use this.

    0 讨论(0)
  • 2020-11-27 04:00

    You can use the Directory.GetFiles method

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

    You can specify the search option in this overload.

    TopDirectoryOnly: Includes only the current directory in a search.

    AllDirectories: Includes the current directory and all the subdirectories in a search operation. This option includes reparse points like mounted drives and symbolic links in the search.

    // 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;
    
    0 讨论(0)
  • 2020-11-27 04:02

    The slickest method woud be to use LINQ:

    var fileCount = (from file in Directory.EnumerateFiles(@"H:\iPod_Control\Music", "*.mp3", SearchOption.AllDirectories)
                            select file).Count();
    
    0 讨论(0)
  • 2020-11-27 04:09

    Reading PDF files from a directory:

    var list = Directory.GetFiles(@"C:\ScanPDF", "*.pdf");
    if (list.Length > 0)
    {
    
    }
    
    0 讨论(0)
  • 2020-11-27 04:10
    int filesCount = Directory.EnumerateFiles(Directory).Count();
    
    0 讨论(0)
提交回复
热议问题