In the. NET environment, all classes about file operations are under the System.IO namespace, note: Security is especially important when modifying files, but this essay is about security, assuming we have sufficient permissions.
1. Managing the file system
Get a picture of the key classes of the entire file system first
(1), System.MarshalByRefObject class: this is. The Base object class for remote operations in the net class, which allows data to be grouped between application domains.
(2), FileSystemInfo class: This is the base class for any file system object
(3), Path class: This class contains static members that can be used to handle path names
(4), DriveInfo class: Its properties and methods provide information about the specified drive
(5), directory, and DirectoryInfo classes: These classes represent folders on a file system
(6), FileInfo, and file classes: These classes represent files on a file system
There are two classes that represent folders, two classes that represent files, depending on the number of times the folder and file is accessed.
(7), the difference between directory class and file class and DirectoryInfo and FileInfo classes
The two classes of the directory class and the file class are static classes, so they cannot be instantiated, and if you only perform an operation on a file or folder, it is good to use these two classes because you can eliminate the overhead of instantiation.
The DirectoryInfo and FileInfo classes implement public methods that are roughly the same as the directory class and the file class, and have some public properties and constructors, but they are stateful and the members of those classes are not static. These classes need to be instantiated. If an object performs multiple operations, it is convenient to use these two classes. Because at construction time, the identity of the appropriate file system object and some other information will be read, no matter how many methods the object (class instance) calls, there is no need to read the information again. Compared with. The directory class and the file class two stateless classes need to read the corresponding file system information again.
(8) The difference between the file class and the FileInfo class
The following is a simple copy of the file to compare the difference between the two, the code is as follows:
FileInfo class
Stopwatch SW =NewStopwatch (); SW. Start (); FileInfo Info=NewFileInfo (@"c:\users\administrator\desktop\project\mvc\c# Advanced Programming (9th Edition): c#5.0 &. NET 4.5.1_13698151.pdf"); info. CopyTo (@"c:\users\administrator\desktop\test\c# Advanced Programming (9th Edition): c#5.0 &. NET 4.5.1_13698151.pdf",true); SW. Stop (); TimeSpan TS=SW. Elapsed;stringElapsed =string. Format ("{0:00}:{1:00}:{2:00}:{3:00}", TS. Hours, TS. Minutes, TS. Seconds, Ts.milliseconds); Console.WriteLine ("The total running hours is {0}", Elapsed); Console.ReadLine ();
File class
Stopwatch SW =NewStopwatch (); SW. Start (); File.Copy (@"c:\users\administrator\desktop\project\mvc\c# Advanced Programming (9th Edition): c#5.0 &. NET 4.5.1_13698151.pdf",@"c:\users\administrator\desktop\test\c# Advanced Programming (9th Edition): c#5.0 &. NET 4.5.1_13698151.pdf",true); SW. Stop (); TimeSpan TS=SW. Elapsed;stringElapsed =string. Format ("{0:00}:{1:00}:{2:00}:{3:00}", TS. Hours, TS. Minutes, TS. Seconds, Ts.milliseconds); Console.WriteLine ("The total running hours is {0}", Elapsed); Console.ReadLine ();
If you do not know the usage of the Stopwatch class, please refer to stopwatch class.
The code example above finds that the use of static class file programs takes less time because the system eliminates the instantiation of the instantiated time
(9) Exists property
This property, FileInfo and DirectoryInfo, is implemented to determine whether files and folders exist, and if an incoming path represents an object that does not exist, a path is passed when an object is initialized through FileInfo and DirectoryInfo. This method constructs without throwing an exception, but throws an exception the first time the method is called, so it is necessary to check the existence of files and folders through the Exists property, and if you pass through the Exists property bite, you can view the other properties of the current instance. The specific properties are as follows:
Note: Both the creation time and the last access time, and the last write time are writable.
In addition to viewing properties, you can also perform actions on file system objects, such as the following:
The above is only part of the usual operations, not all methods and properties of FileInfo and DirectoryInfo instances. The read-write file is actually done with a stream object, and FileInfo can implement operations such as Open (), OpenRead (), OpenWrite (), OpenText (), Create (), CreateText (), and so on, for which they return a stream object.
(10), the path class is a static class, the class can not be instantiated, it provides a number of static methods, it is easier to operate on the path name, such as the Combine method to support a number of road consolidation, using the path class than manual processing of the individual symbols is much easier, In particular, the path class can handle the pathname on different operating systems, although. NET supports only Windows platforms. Here are some of the properties and methods of the path class
C # File operations