The operation of the C # IO stream is very important, and we use this technique for reading and writing files, which shows an example of a file content copy, briefly explaining IO operations in C #.
namespaceconsoleapplication1{classProgram {Static voidMain (string[] args) { //read the contents of a file into a streamStream stream = File.Open ("Test.txt", FileMode.OpenOrCreate); //initialize an array of bytes byte[] bytes =New byte[(int) stream. Length]; //reads a stream into a byte arrayStream. Read (Bytes,0, Bytes. Length); //Receive with MemoryStreamMemoryStream ms =NewMemoryStream (bytes); //set from StartMs. Seek (0, Seekorigin.begin); //and write the returned MemoryStream to another file .Ms. WriteTo (NewFileStream ("NewFile.txt", FileMode.OpenOrCreate)); } }}
Stream is an abstract class, while MemoryStream and FileStream are subclasses of Sream.
The following example demonstrates the asynchronous way to read the contents of a txt text.
namespaceconsoleapplication1{classProgram {Static voidMain (string[] args) {Console.WriteLine (Gettxt (). Result); } /// <summary> ///Read txt text content asynchronously/// </summary> /// <returns></returns> Public Static Asynctask<string>Gettxt () {using(Stream stream = File.Open ("Test.txt", FileMode.OpenOrCreate)) { using(StreamReader sr =NewStreamReader (Stream, Encoding.default)) { return awaitSr. Readtoendasync (); } } } }}
For more classes and operations on Io Please refer to: Https://msdn.microsoft.com/zh-cn/library/system.io (v=vs.110). aspx.
Operation of C # IO stream