Summary of operation methods of C file directory
- 2021-09-24 23:21:02
- OfStack
Need using System. IO;
1) Relative path to absolute path
string fullfolder = HttpContext.Current.Server.MapPath(folder);
2) File movement (renamed)
File.Move(Server.MapPath("/a.txt"), Server.MapPath("/b.txt"));
3) File copying
File.Copy(Server.MapPath("/a.txt"), Server.MapPath("/b.txt"), true);
4) Does the file exist
File.Exists(filefullname)
5) Does the directory exist
Directory.Exists(fullfolder))
6) Create a directory
Directory.CreateDirectory(fullfolder);
7) Directory Move
Directory.Move
8) Reading a text file
StreamReader srd = File.OpenText(fullfilename);
srd.ReadToEnd();
srd.Close();
srd.Dispose();
9) Write files
StreamWriter swr = File.CreateText(Server.MapPath("test.txt"));
swr.Write("message");
swr.Close();
swr.Dispose();
10) Delete files
//Delete files on hard disk
if (File.Exists(filefullname))
{
File.Delete(filefullname);
}
11) Directory traversal
public void ListFiles(string pathname)
{
//All directories and files
string[] subDirs = Directory.GetDirectories(pathname);
string[] subFiles = Directory.GetFiles(pathname);
foreach (string subDir in subDirs)
{
ListFiles(subDir);
}
//All files
foreach (string subFile in subFiles)
{
string filename = Path.GetFileName(subFile);
}
}
12) File modification time
FileInfo fi = new FileInfo(@"c:\test.txt");
DateTime writetime = fi.LastWriteTime;
13) Extract the file name from the file name with the path
System. IO. Path. GetFileName (fullPath); //Filename