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