On the basic operation of the text file: the file made XGP .Net operating mode is I / O stream, and therefore before the introduction of .NET file operations, we first take a brief look at the concept of flow. Stream is a byte sequence. A stream represents the data flow of input or output to a data source, and the operation of the stream is performed in the order of the byte flow in the stream. According to the flow direction of the data, the flow can be used as two categories: input flow and output flow. The stream from the input device to the computer is generally referred to as input; the flow from the computer to the output device is generally referred to as an output stream. For example, the stream input from the keyboard to the memory is the input stream, the flow from the memory to the display is the output stream. Corresponding to our file read / write operation, the flow of output data from the file is the output stream, and the flow of writing data to the file is the input stream. The .NET framework mainly provides us with a system.io namespace that basically contains all and I / O operations related classes. This article is a simple introduction to the reading, creation, deletion, copying, and mobile operations of text files.
First, find the file information in a directory: The DirectoryInfo class under system.io namespace provides processing and path-related methods, such as establishing, copying, moving, and deleting. The following code looks out all the files in the "E: / TestDir" directory and outputs all file names: DirectoryInfo Dirinfor = New DirectoryInfo (@ "E: / TestDir"); // Take the name of the file in the specified directory "fileInfo [] Files = Dirinfor.GetFiles (); // The number of files int fileestotal = files.length; console.writeline ("Total Number of Bmp Files", FileStotal; for (INT i = 0; i Second, the reading and writing of text files has many read and write operations for files under System.io namespace, where the StreamReader class and the StreamWriter class are most commonly used. When a streamreader (or STREAMWRITER) object is established, a stream of reading (or writing) files is also established, here we don't have to care about this stream, just use streamreader (streamwriter), through The established stream to read (or write) the data in the file. Usually we use the File class OpenText method to get a StreamReader object, which we can implement read operations for text files; use the File class CreateText to get a streamwriter object, through which we can implement the text file operating. After you get the read flow object, you can read and write the file using the readline method of the object, the Readline method is to read a line of characters from the current stream and return the data as a string. After you get the write stream, you can use the WriteLine method of the object to write a string to your file. After reading and writing of the file, the CLOSE () method using the stream is turned off. The following code shows how to read a text file. String linetext = null; streamreader sr = file.opentext (@ "e: /testdir/testfile.txt"); while ((linetext = sr.readline ())! = null) {console.writeline (linetext);} SR .Close (); The following code shows how to write a string to a text file. Streamwriter SW = file.createtext (@ "e: /testdir/newfile.txt"); sw.writeLine ("Hello, PLMM"); sw.close (); 3, creation and deletion of files to create new files can be used Class Create method, you can also use the Create method of the FileInfo class. The following code shows how to create a text file, then access file information such as the creation time, the absolute path of the file. FileInfo Fi = New fileInfo (@ "e: /testdir/newfile.txt"); fi.create (); console.writeline ("CREATION TIME: {0}", FI.CREATIONTIME); console.writeline ("Full Name : {0} ", fi.fullname); Deleting files You can use the Delete method of the FILEINFO class using the FILE type. File.delete (@ "e: /testdir/newfile.txt"); Fourth, the copy of the file and the mobile replication file can use the File class's COPY method, or the COPYTO method for the FileInfo class. The following code uses the copy method of the FILE class to copy the OldFile.txt file. String fromfilename = "e: //testdir/from//oldfile.txt"; string destFileName = "e: //testdir//Dest//newfile.txt"; file.copy (fromfilename, destfilename, true); mobile Files can use the MOVE method of the FILE class, or the MoveTo method for the FileInfo class. The following code uses the File class's Move method to move the TexFile.txt file under the E: / TestDir / from / Directory to the E: / TestDir / Dest Directory. String fromfilename = "e: //testdiR//from//testfile.txt"; string destFileName = "E: //testdir//dest//testfile.txt"; file.move (fromfilename, destfilename); Five, summary : This article is just a brief introduction to the basic knowledge and method of C # file operation. By this article we can have a basic understanding of the system.io namespace, the StreamReader class, the StreamReader class, and the StreamWriter class, FileInfo. I hope this article will help beginners.