Microsoft's. NET Framework provides us with stream-based I/O operations, which greatly simplifies developers' work. Because we can operate on a series of common objects, we don't have to worry about whether the I/O operation is related to local files or network data .. The Net Framework mainly provides a system. Io namespace, which basically contains all classes related to I/O operations.
This article will introduce some basicFile OperationsMethods, including directory and file operations in the file system, as well as file read/write operations. By using system. io. directoryinfo and system. io. the fileinfo class allows you to easily perform operations related to directories and files. io. streamreader class and system. io. streamwriter class allows you to conveniently perform operations related to file read/write.
Namespace Overview
The following table shows the most important classes in the system. Io namespace. By using these classes, we can complete the basicFile Operations.
Table 1
Class Name functions and usage
Binaryreader and binarywriter read and write binary data
Directory, file, directoryinfo, and fileinfo are used to create, delete, and move directories and files, and obtain information about specific directories and files through properties.
Filestream accesses files in random mode
Memorystream accesses data stored in memory
Streamreader and streamwriter read and write text data
Stringreader and stringwriter use string buffering to read and write text data.
Use the directoryinfo class and fileinfo class
The base classes of the directoryinfo and fileinfo classes are both filesysteminfo classes. This class is an abstract class. That is to say, you cannot instantiate this class. You can only generate and instantiate its subclass by inheriting it. However, you can use the attributes defined by the class. The following table shows the attributes defined by the class.
Table 2
Attribute functions and usage
Attributes returns file-related attribute values, which use the fileattributes Enumeration type values.
Creationtime returns the File Creation Time
Exists checks whether the file exists in the specified directory.
Extension: the extension of the returned File
Lastaccesstime: Last access time of the returned object
Fullname indicates the absolute path of the returned object.
Lastwritetime: The last write operation time of the returned object
Name: returns the name of the specified file.
Delete () is a method for deleting an object. Please use this method with caution.
The directoryinfo class provides methods for creating, deleting, and moving directories. To use various attributes in Table 2, we must first create a directoryinfo class object and then access its various attributes.
Directoryinfodir1 = newdirectoryinfo (@ "F: test ");
Console. writeline ("fullnameis: {0}", dir1.fullname );
Console. writeline ("attributesare: {0}", dir1.attributes. tostring ());
At the same time, we can also use the fileattributes Enumeration type value to obtain various file-related attributes. The following table shows the values of this enumeration type.
Table 3
Value functions and usage
Archive
Compressed: whether the returned file is compressed
Whether the file returned by directory is a directory
Encrypted: whether the returned file is encrypted
Indicates whether the returned file is hidden.
Offline indicates that file data is not available.
Readonly indicates that the file is read-only.
System indicates that the file is a system file
DirectoryFile Operations
Using the directoryinfo class object, we can easily perform operations on directories and files in directories. If you want to obtain all BMP files in a directory F: picturesCodeYou can achieve this function.
Directoryinfodir = newdirectoryinfo (@ "F: Pictures ");
Fileinfo [] BMP files = dir. getfiles ("*. BMP );
Console. writeline ("totalnumberofbmp Files", BMP files. Length );
Foreach (fileinfofinbmp files)
{
Console. writeline ("nameis: {0}", F. Name );
Console. writeline ("lengthofthefileis: {0}", F. Length );
Console. writeline ("creationtimeis: {0}", F. creationtime );
Console. writeline ("attributesofthefileare: {0 }",
F. Attributes. tostring ());
}
In the above Code, we first create a directoryinfo object, and then call the getfiles method of this object to obtain all files with the BMP extension under the F: Pictures directory, the value returned by this method is a fileinfo array, and each element represents a file. Finally,ProgramRelated Properties of each BMP file are also listed.
Create subdirectory
It is very easy to use the directoryinfo class to create sub-directories. You only need to call the createsubdirectory () method. The Demo code is as follows.
Directoryinfodir = newdirectoryinfo (@ "F: Pictures ");
Try
{
Dir. createsubdirectory ("sub ");
Dir. createsubdirectory (@ "submysub ");
}
Catch (io1_tione)
{
Console. writeline (E. Message );
}
Use the fileinfo class to create and delete files
Through the fileinfo class, we can easily create a file, access the file attributes, and perform basic operations on the file, such as opening the file, closing the file, and reading and writing the file. The following code shows how to create a text file and access the file information such as its creation time, the absolute path of the file, and the file attributes. Finally, the program provides a method to delete the file.
Fileinfofi = newfileinfo (@ "F: myprogram.txt ");
Filestreamfs = Fi. Create ();
Console. writeline ("creationtime: {0}", Fi. creationtime );
Console. writeline ("fullname: {0}", Fi. fullname );
Console. writeline ("fileattributes: {0}", Fi. Attributes. tostring ());
Console. writeline ("pressanykeytodeletethefile ");
Console. Read ();
Fstr. Close ();
Fi. Delete ();
Understanding the open () method of the fileinfo class
We must open the file before performing read/write operations on the file. The fileinfo class provides us with an open () method that contains two Enumeration type values, one is the filemode Enumeration type value, and the other is the fileaccess Enumeration type value. By setting these two parameter values, we can control the file access mode and operation permissions. The following two tables show the values of the filemode Enumeration type and the fileaccess Enumeration type respectively.
Table 4
Value functions and usage
Append: open the file and add data. When this method is used, the value of the fileaccess Enumeration type should be write.
Create creates a new file, which may overwrite existing files.
Createnew creates a new file. If the file already exists, an ioexception is thrown.
Open open an existing file.
Openorcreate open the file. If the file does not exist, create it.
Truncate truncates an existing file.
Table 5
Value functions and usage
Read CAN read data from a file.
Readwrite can read data from a file and write data to the file.
Write can write data to the file.
The following code shows how to use the open () method.
[Highlight] fileinfof = newfileinfo ("F: myfile.txt ");
Filestreams = f. Open (filemode. openorwrite, fileaccess. Read );
[/Highlight]
Use the streamreader and streamwriter classes to read and write files.
Read/write operations on files should be the most importantFile OperationsThe system. Io namespace provides many file read/write operation classes. Here I will introduce you to the most common and basic streamreader and streamwriter classes. From the names of these two classes, we can easily find that they are both stream-based read/write operation classes.
We can use the opentext () method of the file class to obtain a streamreader object. With this object, we can perform read operations on text files by using the following methods:
Console. writeline ("readingthecontentsfromthefile ");
Streamreaders = file. opentext ("mytext.txt ");
Stringread = NULL;
While (read = S. Readline ())! = NULL)
{
Console. writeline (read );
}
S. Close ();
By calling the createtext () method of the fileinfo class, we can obtain a streamwriter object. by calling the writeline () method of the streamwriter class, we can write data to the text file. The method is as follows:
Fileinfof = newfileinfo ("mytext.txt ")
Streamwriterw = f. createtext ();
W. writeline ("thisisfrom ");
W. writeline ("Chapter1 ");
W. writeline ("C #Module ");
W. Write (W. newline );
W. writeline ("thanksforyourtime ");
W. Close ();
Summary
I briefly introduced the aboveC #File OperationsThrough this article, you can easily find the ease of I/O operations under the. NET Framework. After learning this articleFile OperationsYou must have a basic understanding of the directoryinfo, fileinfo, filestream, streamreader, and streamwriter classes in the system. Io namespace and use them flexibly in practical applications. If you wantFile OperationsWith further control, you may wish to study the more specific and detailed classes in the system. Io namespace. Finally, I hope this article will help you.