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 #.
Namespace consoleapplication1{ class program { static void Main (string[] args) { //reads the contents of the file into the stream Stream stream = File.Open ("test.txt", FileMode.OpenOrCreate); Initializes a byte array of byte[] bytes = new byte[(int) stream. Length]; Reads a stream into a byte array of stream. Read (bytes, 0, bytes. Length); Receive MemoryStream ms = new MemoryStream (bytes) with MemoryStream; Set MS at the beginning . Seek (0, seekorigin.begin); Then write the returned MemoryStream to another file to Ms. WriteTo (New FileStream ("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.
Namespace consoleapplication1{ class program { static void Main (string[] args) { Console.WriteLine (Gettxt (). Result); } <summary>/// read txt text content asynchronously///</summary>// <returns></returns> public static async task<string> Gettxt () { using (Stream stream = File.Open ("Test.txt", FileMode.OpenOrCreate) { using (StreamReader sr = new StreamReader (stream, Encoding.default)) { return await Sr. 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