Introduction to CSharp file reading and writing,

Source: Internet
Author: User
Tags visual studio 2010

Introduction to CSharp file reading and writing,
C # is an object-oriented high-level programming language that runs on. NET Framework. It is scheduled to make its debut at the Microsoft Professional Developer Forum (PDC. C # is the latest achievement of Microsoft researcher Anders Hejlsberg. C # Looks surprisingly similar to Java. It includes a single inheritance, an interface, almost the same syntax as Java, and the process of compiling to intermediate code before running. However, C # is obviously different from Java. It draws on a feature of Delphi and is directly integrated with COM (Component Object Model) and is a Microsoft Company. NET windows network framework.

It is very convenient to use c # To read and write files. Next we will illustrate it with a very simple example.

Start Visual Studio 2010, create a project, select Visual c # Windows form application as the template, enter the project name, select create directory for solution, and click OK.


Oh, an error is reported because the project name contains #. Change it to CSharp and click OK.


After the project is successfully created, the form design view is displayed by default. move the cursor to the lower right corner of the Form. When the shape of the mouse pointer changes, you can press the left mouse button and drag it to change the window size.


In the Properties window on the right, you can set the title of the window and the position of the window on the screen when the program starts.


Click the Toolbox on the left, select Textbox, and drag a Textbox from the form by pressing the left mouse button.


In the Textbox selected status, you can set its properties in the Properties window on the right, such as Multiline = true and ScrollBars = Both.


Click the Button in the toolbox, draw two buttons in the window, select the Button, and modify the text of the Button in the Properties window on the right to read and save the file.


Right-click the project name, select Add item, and add a text file.


Enter some content in the newly added file for testing.


Set the file compilation option in the Properties window on the right.


Double-click the Read File button to add the RESPONSE event processing code:

/// <Summary> /// read the file /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> private void button#click (object sender, eventArgs e) {try {FileStream fs = new FileStream ("test. bat ", FileMode. openOrCreate); StreamReader sr = new StreamReader (fs); textBox1.Text = sr. readToEnd (); sr. close (); fs. close (); fs. dispose ();} catch (Exception ex) {MessageBox. show (ex. message );}}
Double-click the save file button to add the RESPONSE event processing code:

/// <Summary> /// save the file /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> private void button2_Click (object sender, eventArgs e) {try {FileStream fs = new FileStream ("test. bat ", FileMode. openOrCreate); StreamWriter sw = new StreamWriter (fs); sw. write (textBox1.Text); sw. close (); fs. close (); fs. dispose (); MessageBox. show ("file saved successfully! ") ;}Catch (Exception ex) {MessageBox. Show (ex. Message );}}
Compile and run the program. Click the Read File button to display the file content in the TextBox. Click the Save button to save the content in the Textbox to the file.
Try it now.

Source code: http://download.csdn.net/detail/testcs_dn/8057357
This article Baidu experience version: http://jingyan.baidu.com/article/22a299b5254acd9e19376ae5.html




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.