Home > Backend Development > C#.Net Tutorial > List files recursively in C#

List files recursively in C#

WBOY
Release: 2023-09-01 08:29:02
forward
1140 people have browsed it

在 C# 中递归列出文件

To get the list of files in a directory, use the SearchOptions.AllDirectories in C#.

Firstly, set the directory for which you want the files −

string[] myFiles = Directory.GetFiles("D:\New\", "*.*", SearchOption.AllDirectories);
Copy after login

The following is an example displaying files from the above mentioned directory −

Example

using System;
using System.Linq;
using System.IO;
class Program {
   static void Main() {
      string[] myFiles = Directory.GetFiles("D:\New\", "*.*", SearchOption.AllDirectories);
      foreach (string res in myFiles) {
         Console.WriteLine(res);
      }
   }
}
Copy after login

Output

The following is the output result. It lists all directories of a folder.

D:\New\one.txt
D:\New\two.html
D:\Newature.png
Copy after login

The above is the detailed content of List files recursively in C#. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template