SoFunction
Updated on 2025-04-08

Summary of operation methods for C# file directory

Need to use;

1) Relative path to absolute path

string fullfolder = (folder);

2) File movement (renamed)

(("/"), ("/"));

3) File Copy

(("/"), ("/"), true);

4) Whether the file exists

(filefullname)

5) Whether the directory exists

(fullfolder))

6) Create a directory

(fullfolder);

7) Directory Move

8) Read text file

StreamReader srd = (fullfilename);
();
();
();

9) Write a file

StreamWriter swr = ((""));
("message");
();
();

10) Delete the file

// Delete files on the hard drive
if ((filefullname))
{
    (filefullname);
}

11) Directory traversal

public void ListFiles(string pathname)
{
// All directories and files
    string[] subDirs = (pathname);
    string[] subFiles = (pathname);
    foreach (string subDir in subDirs)
    {
        ListFiles(subDir);
    }
// All files
    foreach (string subFile in subFiles)
    {
        string filename = (subFile);
    }
}

12) File modification time

FileInfo fi = new FileInfo(@"c:\");
DateTime writetime = ;

13) Extract file name from file name with path

(fullPath);//File name