C # Reading and Writing text files

Source: Internet
Author: User

<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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.