09-Delete directory and file operations

Source: Internet
Author: User

Basic knowledge of file operations:

Directory //Operations Directories (folders), static classes. DirectoryInfo//folder of a "class", instance class. Used to describe a folder object that returns a DirectoryInfo array when all directories under the specified directory are obtained. Returns the folder array)FileInfo//File class, instance class. Used to describe a file object. Returns an FileInfo array when all the files in the specified directory are obtained.

Example: Delete files in all folders and folders under F:\a:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.IO;namespace_06 Delete Folder Action {classProgram {Static voidMain (string[] args) {            //============== Delete files in all folders and folders under F:\a ===========            #region1. The most direct approachstring[] Demodirs = Directory.getdirectories (@"f:\a");//get all folders in this directory            string[] Demofiles = Directory.GetFiles (@"f:\a");//get all the files in this directory//Delete Folder Action            if(Demodirs.length >0)            {                foreach(varIteminchdemodirs) {Directory.delete (item,true);//True indicates that a subfolder is not empty when it is also deleted                }            }            //Delete file Operation            if(Demofiles.length >0)            {                foreach(varIteminchdemofiles)                {File.delete (item); }            }             #endregion        }    }}

Suppose Directory.delete (item, TRUE), cannot use true.

Idea: Using recursive method, first delete the file under each directory, and then delete the folder, the code is as follows:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.IO;namespace_06 Delete Folder Action {classProgram {Static voidMain (string[] args) {            //============== Delete files in all folders and folders under F:\a ===========//2. DirectoryInfo operation folder class, FileInfo operation File class, recursiveDirectoryInfo Dirinfo =NewDirectoryInfo (@"f:\a");            Deletedir (Dirinfo); Dirinfo.delete ();//Finally, delete the directory        }        Static voidDeletedir (DirectoryInfo dirinfo) {directoryinfo[] Dirinfos=dirinfo.getdirectories (); Fileinfo[] Fileinfos=Dirinfo.getfiles (); if(Fileinfos.length >0)            {                foreach(varFileinchFileinfos) {file. Delete ();//Delete files under this directory                }            }            if(Dirinfos.length >0)            {                foreach(varDirinchDirinfos) {Deletedir (dir);//recursively call this method to delete folders and files in this directoryDir. Delete ();//Delete this directory                }            }        }    }}

Summary of use of the Directory and DirectoryInfo methods:
1. Create a few directories: Directory.CreateDirectory (@ "c:\test\1");
2. Get all the immediate subdirectories under the current directory:
string[] dirs = directory.getdirectories (@ "C: \");
string[] dirs = directory.getdirectories (@ "C: \", "*i*", searchoption.topdirectoryonly); Wildcard to find subdirectories under the directory, you can search for hidden files.
3. Determine if a folder exists in a directory: BOOL Directory.Exists (PATH)
4. Delete the directory, recursive indicates whether to delete recursively, and if recursive is false, only empty directories can be deleted:
void Delete (string path, bool recursive)
5. to get a file under a directory:
String[] Directory.GetFiles (string path)

String[] Directory.GetFiles (string path, String searchpattern, SearchOption searchoption), wildcard find file under directory

6. Move ()//movement, cut. Only on the same disk. The directory does not have a copy method. You can use the move () method to implement renaming.

09-Delete directory and file operations

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.