SoFunction
Updated on 2025-03-06

C# methods for reading, writing, creating, copying, moving, and deleting files

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 +"&lt;/b&gt;&lt;br/&gt;");
  DirectoryInfo directoryinfo = new DirectoryInfo(item);
  ("name:"+ +"&lt;br/&gt;");
  ("path:"+ +"&lt;br/&gt;");
  ("Create time:"+ +"&lt;br/&gt;");
  ("Last visit time:"++"&lt;br/&gt;");
  ("Last modified time:"++"&lt;br/&gt;");
  ("Parent folder:"++"&lt;br/&gt;");
  ("Root directory:"++"&lt;br/&gt;");
  ("&lt;br/&gt;");
 }

 //All subfiles foreach (string item (("a")))
 {
  ("<b>File:" + item +"&lt;/b&gt;&lt;br/&gt;");
  FileInfo fileinfo = new FileInfo(item);
  ("name:" +  +"&lt;br/&gt;");
  ("Extension:" + +"&lt;br/&gt;");
  ("path:" + +"&lt;br/&gt;");
  ("size:" + +"&lt;br/&gt;");
  ("Create time:" + +"&lt;br/&gt;");
  ("Last visit time:" + +"&lt;br/&gt;");
  ("Last modified time:" + +"&lt;br/&gt;");
  ("Folder:" + +"&lt;br/&gt;");
  ("File Properties:" + +"&lt;br/&gt;");
  ("&lt;br/&gt;");
 }
}

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.