We write some data in textbox and it will be save in a text file in the given path.
Initial chamber
Step 1: Open Visual Studio 2010, Go to File, New, Projects, then under Visual C# select Windows.
You can change the name of the project and browse your project to different location too. And then press – OK.
Now go to any drive, make a folder demo and save a demo.txt file.
Design chamber
Step 2: Now open your Form1.cs file, where we create our design for inserting data into Text File. We will drag one Textbox and a button to Form1.cs. You will see your Form look like this.
Form1.cs [design]:
Change the property of textbox - Multiline as True.
Code chamber
Right click on the blank part of Form1.cs and View Code. You will see you are entered in the code part of the form. Write the following code and then Press F5 to run the project.
Form1.cs:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
-
- namespace WindowsFormsApplication2
- {
- public partial class Form1: Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- TextWriter txt = new StreamWriter("C:\\demo\\demo.txt");
- txt.Write(textBox1.Text);
- txt.Close();
-
- }
- }
- }
Output chamber Save to text file