The file operation classes provided in the System.IO namespace have files and FileInfo, and the functions of the two classes are basically the same, except that file is a static class, where all methods are static and can be called directly through the class name and do not need to be instantiated. Whereas FileInfo is a normal class, the method can be called only after the object is instantiated.
The file class is a static class that operates on the entire filesystem and is static, and it is recommended that you use the file class to avoid the overhead of frequently creating and releasing objects if you are only doing a small amount of manipulation of the files, such as determining whether a file exists or working on many files.
The FileInfo class is an instance class that operates on a file, most of which is an instance method, and its operation may be the corresponding static method in the called file. If you are doing a lot of work on a file, we recommend that you use the FileInfo class.
So why do you sometimes use FileInfo? Because every time a method is called through the file class, it takes a certain amount of CPU processing time to perform security checks, even if the same file is repeatedly accessed using a different method of the file class. The FileInfo class performs a security check only when the FileInfo object is created.
for ease of operation, there are many times when it is not necessary to generate a FileInfo object so troublesome.
For example: Copy a file to another place:
file.copy (Filepath,newfilepath) can.
If you use FileInfo
may require:
fileinof fi = new FileInfo ();
fi. CopyTo (Newfilepath);
we can use the following rules to determine:
1. If the application performs several operations on a file, it is better to use the FileInfo class because the correct file is already referenced when the object is created, and the static class takes more time to find the file each time.
2, if a single method call, it is recommended to use the file class, do not have to instantiate the object.
The main difference between file and FileInfo classes in C # files class