C # traverse all files in a folder, including subfolders

Source: Internet
Author: User

Using System; using System. IO; class ListAllFilesDemo {public static void Main () {Console. write ("Enter the directory to be queried:"); string dir = Console. readLine (); try {ListFiles (new DirectoryInfo (dir);} catch (IOException e) {Console. writeLine (e. message) ;}} public static void ListFiles (FileSystemInfo info) {if (! Info. exists) return; DirectoryInfo dir = info as DirectoryInfo; // if (dir = null) return; FileSystemInfo [] files = dir. getFileSystemInfos (); for (int I = 0; I <files. length; I ++) {FileInfo file = files [I] as FileInfo; // if (file! = Null) Console. writeLine (file. fullName + "\ t" + file. length); // For subdirectories, recursively call else ListFiles (files [I]) ;}} C # to traverse the files in the directory (2) 1. to traverse all directories under a directory, use System. IO. the GetDirectories method of the DirectoryInfo class: DirectoryInfo dir = new DirectoryInfo (@ "c: \"); foreach (DirectoryInfo dChild in dir. getDirectories ("*") {// If GetDirectories ("AB *") is used, all directories starting with AB will be displayed as Response. write (dChild. name + "<BR>"); // print the directory Name Response. write (d Child. fullName + "<BR>"); // print the path and directory name} 2. traverse all files in a directory and use System. IO. the GetFiles method of the DirectoryInfo class: DirectoryInfo dir = new DirectoryInfo (@ "c: \"); foreach (DirectoryInfo dChild in dir. getFiles ("*") {// If GetFiles ("*. txt "), then all txt files will be displayed Response. write (dChild. name + "<BR>"); // print the file Name Response. write (dChild. fullName + "<BR>"); // print the path and file name} How to obtain the files and subdirectories contained in the specified directory 1. directoryInfo. getFiles (): Get the files in the directory (excluding subdirectories). The return type is Fil. EInfo [], supports wildcard search; 2. directoryInfo. getDirectories (): Obtain the subdirectory of the directory (excluding the subdirectory). The return type is DirectoryInfo []. wildcard search is supported. 3. directoryInfo. getFileSystemInfos (): Get the files and subdirectories under the specified directory (excluding subdirectories). The return type is FileSystemInfo []. wildcard search is supported. How to obtain the basic information of the specified file? FileInfo. exists: Get whether the specified file Exists; FileInfo. name, FileInfo. extensioin: get the file name and extension; FileInfo. fullName: Get the fully qualified name of the file (full path); FileInfo. directory: Directory where the file is located. The returned type is DirectoryInfo and FileInfo. directoryName: Get the path of the directory where the file is located (after Full path); FileInfo. length: get the file size (number of bytes); FileInfo. isReadOnly: Get whether the file is read-only; www.2cto.com FileInfo. attributes: gets or sets the Attributes of a specified file. The returned type is FileAttributes enumeration. It can be a combination of multiple values, FileInfo. creationTime, FileInfo. lastAccessTime, FileInfo. lastWriteTime: used to obtain the file creation time, access time, and modification time. It uses recursive traversal to traverse all files in folders and subfiles. Public void FindFile (string dirPath) // The dirPath parameter is the specified directory {// search for files under the specified directory and subdirectory, list subdirectories and file DirectoryInfo Dir = new DirectoryInfo (dirPath) in listBox1; try {foreach (DirectoryInfo d in Dir. getDirectories () // find the subdirectory {FindFile (Dir + d. toString () + "\"); listBox1.Items. add (Dir + d. toString () + "\"); // Add the directory name in listBox1} foreach (FileInfo f in Dir. getFiles ("*. --- ") // find the file {listBox1.Items. add (Dir + f. toString (); // Add the file name in listBox1} catch (Exception e) {MessageBox. show (e. message) ;}} use the following code to limit the file type: foreach (FileInfo f in Dir. getFiles ("*. --- ") // find the file" *. --- "indicates the extension of the file type to be accessed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.