<Span style = "white-space: pre"> </span> Import namespace: <span style = "white-space: pre"> </span> using System. IO; # region reads FileStream fs; // declares the object StreamReader sr of the file stream; // declares the object StreamWriter sw of the reader; // declares the object string str of the writer; private void btn read _ Click (object sender, EventArgs e) {openFD dialog box. multiselect = false; // The openFD dialog box for multiple files is not allowed. filter = "text file | *. txt "; // file type string path = null; if (openFD dialog box. showDialog () = DialogResu Lt. OK) {path = openFD dialog box. fileName; if (path. equals (null) | path. equals ("") {MessageBox. show ("select file"); return;} txt file location. text = path; try {// create file stream fs = new FileStream (path, // file path FileMode. open, // file opening method FileAccess. readWrite, // control the read/write FileShare of the object. none); // control access to this file by other processes // create a reader sr = new StreamReader (fs, // file stream object Encoding. default); // character encoding str = sr. readToEnd (); // read all txt text in the file. text = str;} catc H (Exception ex) {MessageBox. Show ("File Operation Exception:" + ex. Message);} finally {if (fs! = Null) {sr. close (); // Close the reader fs. close (); // Close file stream }}# endregion # region write private void btn write _ Click (object sender, EventArgs e) {string path = txt file location. text; string text = txt Text. text; if (path. equals (null) | path. equals ("") {MessageBox. show ("file path cannot be blank"); return;} try {fs = new FileStream (path, FileMode. create, FileAccess. readWrite, FileShare. none); // create a writer sw = new StreamWriter (fs); // The parameter is a file stream. Object sw. write (text); MessageBox. show ("written successfully");} catch (Exception ex) {MessageBox. show ("File Operation exception:" + ex. message);} finally {if (fs! = Null) {sw. Close (); fs. Close () ;}}# endregion