Here's a little bit of learning about file knowledge in C #, this article briefly describes several classes of file systems that exist in the System.IO namespace, incidentally, the file class reads and writes, and the next article describes the operation of the stream. Knowing the knowledge of files and streams can be helpful in file operations developed by Windows Phone.
C # is an object-oriented language, for beginners, may not be able to clear the various classes, in fact, mainly some of the inheritance of classes, after inheriting the implementation of each class their own unique functions. So the inheritance relationship can help us to understand a lot of why. There may also be a need for a little polymorphic concept.
- 1. Inheritance Relationship
<< file System Class-Drawing .vsdx>>
====================== Description =====================================
System.MarshalByRefObject
This is. NET class for remote operations, and I don't know much about it, see this article http://dudu.cnblogs.com/archive/2004/03/04/2182.html
FileSystemInfo
A base class that can represent any file system object
FileInfo and file
Represents a file on a file system, the difference is that the file class is a static class, and if you perform an operation, you can eliminate the overhead of instantiating an object, and FileInfo performs multiple operations with an object.
DirectoryInfo and Directory
Represents a folder on a file system, the same as above.
Path
Processing path names
DriveInfo
Drive information
- 2.FileInfo DirectoryInfo File Directory Path class
Introduce FileInfo and DirectoryInfo class
New FileInfo (@ "D:\demo.txt"); Myfile.copyto (@ "E:\demo.txt"
The following code has the same effect
File.Copy (@ "D:\demo.txt",@ "E:\demo.txt"
The first code executes a little bit longer because a FileInfo object myfile needs to be instantiated, but myfile can continue to do more, and the second segment does not need to instantiate an object.
As you can see from the example above, you can instantiate an object as long as you pass the path string containing the file system object to the constructor, and the following is an instance of the folder
New DirectoryInfo (@ "D:\demofolder"
So the question is, if you give the file a path, what happens to the path to a file?
The answer is no error, only when the method is called, if you call the task it cannot complete, it throws an exception. In fact, the FileInfo and DirectoryInfo objects have a property exists indicates whether they exist, for example:
New FileInfo (@ "C:\Windows"
This code will output false if the test is called. Open () opens the folder as a file opens, creating an exception.
In addition, these two classes have many properties, methods to determine the object's information, to perform operations, not listed here.
In the properties, there is the creation time, the last access time, the last write time, these properties can be written oh, the meaning of writing is to delete the old file, replace the old file with the new content, you can modify the creation date to match the original file creation date.
Then the path class
The path class is a static class and cannot be instantiated, and it provides some static methods that can easily operate on the path name. For example:
Console.WriteLine (Path.Combine (@ "D:\demofolder","demo.txt"
This code shows the full path name.
- 3. File reading and writing
File read and write is very simple, but not done by the DirectoryInfo FileInfo class described above. In. NET, you can read and write files through a file object, which is essentially a stream to read and write files.
The file class has a number of options for you to choose from:
File.readalltext (FilePath); // file.readalltext (filepath,encoding)//
For example:
And also:
ReadAllBytes ()//ReadAllLines ()//
There are read and write, method names are relative.
. Net File System Classes