This article describes the methods of C# to read, write, create, copy, move and delete files. Share it for your reference. The specific analysis is as follows:
1. Folder creation, movement, and deletion
//Create a folder(("a")); (("b")); (("c")); //Move b to a(("b"), ("a//b")); //Delete c(("c"));
2. File creation, copying, moving, deleting
//Create a file//When using create and then copy/move/delete, you will prompt://The file is being used by another process, so the process cannot access the file//Use FileStream instead to get//The return is closed and there is no problemFileStream fs; fs = (("")); (); fs = (("")); (); fs = (("")); (); //Copy the file((""),("a//")); //Move files((""),("a//")); ((""),("a//")); //Delete the file((""));
3. Iterate over files and subfolders in folders and display their properties
if((("a"))) { //All subfolders foreach(string item (("a"))) { ("<b>Folder:" + item +"</b><br/>"); DirectoryInfo directoryinfo = new DirectoryInfo(item); ("name:"+ +"<br/>"); ("path:"+ +"<br/>"); ("Create time:"+ +"<br/>"); ("Last visit time:"++"<br/>"); ("Last modified time:"++"<br/>"); ("Parent folder:"++"<br/>"); ("Root directory:"++"<br/>"); ("<br/>"); } //All subfiles foreach (string item (("a"))) { ("<b>File:" + item +"</b><br/>"); FileInfo fileinfo = new FileInfo(item); ("name:" + +"<br/>"); ("Extension:" + +"<br/>"); ("path:" + +"<br/>"); ("size:" + +"<br/>"); ("Create time:" + +"<br/>"); ("Last visit time:" + +"<br/>"); ("Last modified time:" + +"<br/>"); ("Folder:" + +"<br/>"); ("File Properties:" + +"<br/>"); ("<br/>"); } }
4. File reading and writing
if ((("a//"))) { StreamWriter streamwrite = newStreamWriter(("a//")); ("Muzi House"); ("https:///"); ("2008-04-13"); (); StreamReader streamreader = newStreamReader(("a//")); (()); (()); (); }
I hope this article will be helpful to everyone's C# programming.