C Simple Traversal of All Files in a Specified Folder

  • 2021-10-25 07:39:15
  • OfStack

This article example shows how C # simply traverses all the files in a specified folder. Share it for your reference, as follows:

C # traverses all files in the specified folder:


DirectoryInfo TheFolder=new DirectoryInfo(folderFullName);
// Traverse folders 
foreach(DirectoryInfo NextFolder in TheFolder.GetDirectories())
  this.listBox1.Items.Add(NextFolder.Name);
// Traverse a file 
foreach(FileInfo NextFile in TheFolder.GetFiles())
  this.listBox2.Items.Add(NextFile.Name);

How to get the files and subdirectories contained in the specified directory

1. DirectoryInfo. GetFiles (): Gets the files in the directory (without subdirectories), and the return type is FileInfo [], which supports wildcard lookup;

2. DirectoryInfo. GetDirectories (): Gets the subdirectory of the directory (excluding subdirectories), and the return type is DirectoryInfo [], which supports wildcard lookup;

3. DirectoryInfo. GetFileSystemInfos (): Get the files and subdirectories under the specified directory (excluding subdirectories), and the return type is FileSystemInfo [], which supports wildcard lookup;

How to get the basic information of the specified file;

FileInfo. Exists: Gets whether the specified file exists;
FileInfo. Name, FileInfo. Extensioin: Get the name and extension of the file;
FileInfo. FullName: Gets the fully qualified name (full path) of the file;
FileInfo. Directory: Get the file directory, return type is DirectoryInfo;
FileInfo. DirectoryName: Get the path to the directory where the file is located (full path);
FileInfo. Length: Gets the size of the file in bytes;
FileInfo. IsReadOnly: Gets whether the file is read-only;
FileInfo. Attributes: Gets or sets the property of the specified file, with a return type of FileAttributes enumeration, which can be a combination of multiple values
FileInfo. CreationTime, FileInfo. LastAccessTime, FileInfo. LastWriteTime: used to obtain the creation time, access time and modification time of files respectively;

More readers interested in C # can check the topic of this site: "C # Traversal Algorithm and Skills Summary", "C # Programming Thread Use Skills Summary", "C # Operating Excel Skills Summary", "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Data Structure and Algorithm Tutorial", "C # Array Operation Skills Summary" and "C # Object-Oriented Programming Introduction Tutorial"

I hope this article is helpful to everyone's C # programming.


Related articles: