C # for drives, directories, access to files, and recursion

Source: Internet
Author: User

What is said here is not to read and write files, but to get files and directories.

Mainly divided into the three categories of drives, directories and files.

The DriveInfo class is required for the drive to implement

You need to use the DirectoryInfo class for the directory to implement

The FileInfo class is required for the file to be implemented.

Where the FileSystemInfo class can access both files and directories.

See the code below for details.

The first is to access the drive:

    //Demo file Operations    /// <summary>    ///define an action class/// </summary>    classFileclass {/// <summary>        ///Print Drive Information/// </summary>         Public voidDrive () {Try            {                //get all the drives on your computerdriveinfo[] Drive =driveinfo.getdrives (); Console.WriteLine ("disk volume label \ t drive name \ t type \ t format \ t total space \ t free space"); foreach(varVARIABLEinchDrive ) {Console.WriteLine ("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", VARIABLE. Volumelabel, VARIABLE. Name, VARIABLE. DriveType, VARIABLE. Driveformat, VARIABLE. TotalSize, VARIABLE.                Availablefreespace); }            }            Catch(Exception ex) {Console.WriteLine (ex).            Message); }            finally{Console.readkey (); }        } classAAA {Static voidMain (string[] args) {Fileclass Fc=NewFileclass ();          Fc.drive (); //DirectoryInfo dtinfo=new DirectoryInfo ("D://learning "); //Fc.directoryfun (Dtinfo); //Fc.filefun (Dtinfo); //Fc.fileanddirectoryfun (Dtinfo); //Fc.recursioninfo (Dtinfo); //Fc.recurfileexe (Dtinfo); //Console.readkey ();        }    }

This will allow you to access the drive information

Below to access the directory information, first I put my directory file to everyone to see, the total file is "learning", below there are 5 directories and a PDF file, several directories also have files and subdirectories.

Below is a demonstration of how to access directory code under the directory below.

/// <summary>       ///folders in a directory can be traversed only by directories (folders) that do not recognize files. /// </summary>       /// <param name= "Dtinfo" ></param>         Public voidDirectoryfun (DirectoryInfo dtinfo) {//Get subdirectoriesdirectoryinfo[] Arry =dtinfo.getdirectories (); Console.WriteLine (Dtinfo.name+"Sub-directories below are:"); foreach(varVARIABLEinchArry) {Console.WriteLine (VARIABLE).           Name);       } console.readkey (); } classAAA {Static voidMain (string[] args) {Fileclass Fc=NewFileclass (); //fc.drive ();DirectoryInfo dtinfo=NewDirectoryInfo ("d://Learning");           Fc.directoryfun (Dtinfo); //Fc.filefun (Dtinfo); //Fc.fileanddirectoryfun (Dtinfo); //Fc.recursioninfo (Dtinfo); //Fc.recurfileexe (Dtinfo); //Console.readkey ();        }    }

The following shows how to access the directory under the file code below

 /// <summary>        ///traversal of files under directory/// </summary>        /// <param name= "Dtinfo" ></param>         Public voidFilefun (DirectoryInfo dtinfo) {//Get FileFileInfo []fileinfo =Dtinfo.getfiles (); Console.WriteLine (Dtinfo.name+"The following files are:"); foreach(varVARIABLEinchfileInfo) {Console.WriteLine (VARIABLE).            Name);        } console.readkey (); }classAAA {Static voidMain (string[] args) {Fileclass Fc=NewFileclass (); //fc.drive ();DirectoryInfo dtinfo=NewDirectoryInfo ("d://Learning"); //Fc.directoryfun (dtinfo);Fc.filefun (Dtinfo); //Fc.fileanddirectoryfun (Dtinfo); //Fc.recursioninfo (Dtinfo); //Fc.recurfileexe (Dtinfo); //Console.readkey ();        }    }

The following shows access to directories and files under the directory, mainly rely on the FileSystemInfo class code as follows

    /// <summary>        ///traversal of files and subdirectories under the directory/// </summary>        /// <param name= "Dtinfo" ></param>         Public voidFileanddirectoryfun (DirectoryInfo dtinfo) {//FileSystemInfo can access both files and directoriesfilesysteminfo[] FileSystemInfo =Dtinfo.getfilesysteminfos (); Console.WriteLine (Dtinfo.name+"the following files and directories are:"); foreach(varVARIABLEinchFileSystemInfo) {Console.WriteLine (VARIABLE).            Name);        } console.readline (); }  classAAA {Static voidMain (string[] args) {Fileclass Fc=NewFileclass (); //fc.drive ();DirectoryInfo dtinfo=NewDirectoryInfo ("d://Learning"); //Fc.directoryfun (Dtinfo); //Fc.filefun (dtinfo);Fc.fileanddirectoryfun (Dtinfo); //Fc.recursioninfo (Dtinfo); //Fc.recurfileexe (Dtinfo); //Console.readkey ();        }    }

The following shows recursively accessing all directories and files under the directory, including subdirectories and files under subdirectories, as shown in the code below.

 /// <summary>        ///recursively access files in all files and directories and subdirectories that are included under a folder/// </summary>        /// <param name= "Dtinfo" ></param>         Public voidRecursioninfo (DirectoryInfo dtinfo) {//get subdirectories and filesfilesysteminfo[] FileSystemInfo =Dtinfo.getfilesysteminfos (); foreach(varVARIABLEinchFileSystemInfo) {                //Print directly if it is a file                if(VARIABLE isFileInfo) {Console.WriteLine (VARIABLE).                Name); }                //if it is a directory, first print the directory name and then access the file and directory recursively                Else                {                    //the directory name is printed firstConsole.WriteLine (VARIABLE.                    Name); //then access the files and directories in the directory recursivelyRecursioninfo ((DirectoryInfo) VARIABLE); }            }                  } classAAA {Static voidMain (string[] args) {Fileclass Fc=NewFileclass (); //fc.drive ();DirectoryInfo dtinfo=NewDirectoryInfo ("d://Learning"); //Fc.directoryfun (Dtinfo); //Fc.filefun (Dtinfo); //Fc.fileanddirectoryfun (dtinfo);Fc.recursioninfo (Dtinfo); //Fc.recurfileexe (Dtinfo); //Console.readkey ();        }    }

The following shows how to access all files of the specified type under the specified directory, still recursive this requires the extension property, which represents the file's extension. The code is as follows

/// <summary>        ///recursive access to all EXE files in a directory requires a extension (file name extension)/// </summary>        /// <param name= "Dtinfo" ></param>         Public voidRecurfileexe (DirectoryInfo dtinfo) {//get all directories and filesFilesysteminfo[] Filesysteminfos=Dtinfo.getfilesysteminfos (); foreach(varVARIABLEinchFilesysteminfos) {                //if it is an EXE file, print it                if(VARIABLE. Extension = =". EXE") {Console.WriteLine (VARIABLE.                Name); }                //If the directory is recursive                Else if(VARIABLE isDirectoryInfo)                {Recurfileexe (DirectoryInfo) VARIABLE); }            }        }classAAA {Static voidMain (string[] args) {Fileclass Fc=NewFileclass (); //fc.drive ();DirectoryInfo dtinfo=NewDirectoryInfo ("d://Learning"); //Fc.directoryfun (Dtinfo); //Fc.filefun (Dtinfo); //Fc.fileanddirectoryfun (Dtinfo); //Fc.recursioninfo (dtinfo);Fc.recurfileexe (Dtinfo);        Console.readkey (); }    }

The above is about the drive, directory and file access, relatively simple, we hope to be helpful.

C # for drives, directories, access to files, and recursion

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.