SoFunction
Updated on 2025-03-07

Detailed explanation of the method of implementing file operations (copy, move, delete) in C#

The File class provides common file operation functions, including copying, moving, deleting, creating shortcuts and other functions as well as reading and setting file attributes.

File Operation

We have to deal with files every day, and common file operations are nothing more than creating, copying, moving, deleting and creating shortcuts.

Through some columns Create and CreateText functions, File provides the function of creating files, which is used to create UTF-8 encoded text files, whose return value is a file handle.

var f1 = ("");
var f2 = ("");
();
();

After running, two files will be generated in the run directory, namely, , and the default return formats of the two are FileStream and StreamWriter respectively.

Based on these two files, the following operations are performed through the functions in the File class.

operate Judge existence copy move delete
function Exists Copy Move Delete
if ((""))
    ("The Sutra exists");

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

("");
if (!(""))
    ("Deleted");

The output results of the command line are as follows. In addition, the run directory isMoved to the upper directory.

The sutra exists
Deleted

Link

The CreateSymbolicLink function can create a link, and the ResolveLinkTarget can get the address pointed to by the shortcut.

Still take it as an example

var f1 = ("");
();
(@"testLink", "");
var s = (@"testLink", true);
();

Due to system restrictions, after generating the software, you need to open it through administrator mode. You can see a new shortcut called testLink, pointing to. Then call ResolveLinkTarget to return this file, and the output result of the command line is

C:\Users\Laser\Documents\00\0505\jsonTest\jsonTest\bin\Debug\net6.0\

File properties

Read set up
File properties GetAttributes SetAttributes
Creation time GetCreationTime GetCreationTime
Last visit time GetLastAccessTime SetLastAccessTime
Last write time GetLastWriteTime SetLastWriteTime
Unix file mode GetUnixFileMode SetUnixFileMode

Among them, the file attribute is an enumeration type. You can see it by entering the source code. 1 is read-only ReadOnly, 2 is hidden Hidden, etc.

Regarding the file creation time, access time and change time, File provides two sets of functions for reading and setting, taking local time and UTC time respectively. All time-related functions given in the above table can be added to Utc later to represent coordinated world time. For example, GetCreationTime has the corresponding GetCreationTimeUtc function.

Here is a brief test of some functions

((""));
((""));
((""));

The output is as follows

Archive
2023/5/14 16:40:42
2023/5/14 8:40:42

This is the article about the detailed explanation of how to implement file operations (copy, move, delete) in C#. For more related C# file operations, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!