C # FileStream read-write text manipulation Code Analysis _c# Tutorial

Source: Internet
Author: User
Tags exception handling

The FileStream object represents a stream that points to a file on a disk or network path.

You can use the FileStream class to read, write, open, close, and so on files on a file system.

Nonsense don't say, start operation.

1. Drag the control, must drop, in addition to the text box to write the file, the other text box ReadOnly properties are set to True!

2. Start the Select File operation

Privatevoid Btnreadchoose_click (object sender, EventArgs e)
    {
//Create Open File dialog box, you can drag the control directly ha ...
      OpenFileDialog ofd=new OpenFileDialog ();
Displays the file path in the text box
if (OFD). ShowDialog () = = DialogResult.OK)
      {
        Txtreadpath.text = ofd. FileName;
      }
    }



Effect Chart ... The path is displayed.

3. Start the "read out" operation and display the text content in the text box.

First, write a way to show "warm tip" messages.

#region Show a warm message +void msgshow (string Msg)
///<summary>
///message with warm tips
///</summary>
// /<param name= "MSG" > Message </param>
void Msgshow (string Msg)
    {
      lbresult.text = "Warm tip:" +MSG;
    } 
#endregion

Read out operation

Privatevoid Btnreadfile_click (object sender, EventArgs e)
    {
//create file stream, stream operation to perform exception handling, and use to release object
try
      {
using (FileStream FS =new FileStream (Txtreadpath.text, FileMode.Open))
        {
//define buffer, 3M size
Byte[] Arrfiledata =newbyte[1024*1024*3];
The read file exists in Arrfiledata
          FS. Read (arrfiledata, 0, arrfiledata.length);
The transcoding format is displayed in the text box txtreadmsg
string changetostring = Encoding.Default.GetString (arrfiledata);
          Txtreadmsg.text = changetostring;
          Msgshow ("text read succeeded!!!");
        }
catch (Exception ex)
      {
        MessageBox.Show ("exception:" + ex.) message);
      }

    

Effect Chart:

4. Start the "write" operation, select the file is not cumbersome, almost.

Privatevoid Btnwritefile_click (object sender, EventArgs e)
    {
//Get the data string to write to text
Strwritedata = Txtwritemsg.text;
Try
      {
using (FileStream FS =new FileStream (Txtwritepath.text, FileMode.Create))
        {
//convert to binary write text
byte[] Arrwritedata = Encoding.Default.GetBytes (strwritedata);

Fs. Write (data buffer, write from the first byte, how many)
          fs. Write (arrwritedata, 0, arrwritedata.length);
          Msgshow ("Write text succeeded!!!");
        }
catch (Exception ex)
      {

        MessageBox.Show ("exception:" + ex.) message);
      }

Effect Chart:

Just like that, a file stream FileStream text write, read small program is completed. The main is their own summary, a deeper understanding of the flow of the process, so share this article!

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.