Ch25 file and registry operations (2) -- read and write files

Source: Internet
Author: User

I had an idea earlier, writing a small program that can read documents and find and replace keywords. It is actually very simple. Just recently I saw the file system chapter, this afternoon, I just made one. Here is a summary:

1. Read text files with streamreader and encode them with encoding. default.

Streamreader sr = new streamreader (filepath, encoding. Default); rtbcontent. Text = Sr. readtoend (); Sr. Close (); // release locked Resources

2. Count the characters to be searched. The string. indexof () and for loop conditions are used.

String content = rtbcontent. text. trim (); string key = txtinput. text. trim (); int COUNT = 0; int start; For (START = 0; (START = content. indexof (Key, start)>-1; Start = start + key. length)
{Count ++;} MessageBox. Show ("" + key + "" appears "+ count. tostring () +" times ");

Network Disk address for this applet: http://pan.baidu.com/s/1hqAAZpM

-------------------------------- Summary completed -------------------------------------------------------------------------------------------

1. readall and writeall methods of the file class (. NET Framework 2.0)

2. Concept of stream:

A stream is an object used to transmit data. Data transmission has two directions:

If the data is transmitted from the external source to the program, this is the read stream; if the data is transmitted from the program to the external source during the summer vacation, This is the write stream.

3. filestream class: used to read and write binary files. To construct a filestream instance, you need the following four pieces of information: (. NET Framework 1.x, which is used before the release of 2.0)

File to be accessed

Indicates how to open the file mode-filemode

File Access Method-fileaccess

Shared access-fileshare

4. read/write streamreader and streamwriter

You can associate streamreader with filestream:

Filestream FS = new filestream ("C: \ readme.txt", filemode. Open, fileaccess. Read, fileshare. None );

Streamreader sr = new streamreader (FS );

Obtain streamreader from the fileinfo instance

Fileinfo myfile = new fileinfo ("C: \ readme.txt ");

Streamreader sr = myfile. opentext ();

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.