How to Write Data to a Text File using C#

In this tutorial, i will teach you how to create a program that writes data to a text file using C#. This is my continuation of my other tutorial entitled How to Read Data from a Text File using C#. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. 2. Next, add only one Button named btnSave and labeled it as "Save". Insert also one textbox named txtInput for inputting a text that we will write in the text file and a SaveFileDialog named SaveFileDialog1 to save it as a text file. You must design your interface like this: design 3. In your btnSave, put this code below. Filter your saveFileDialog1 to be saved as a text file or a .txt file extension. And add also a title to this control.
  1.             saveFileDialog1.Filter = "Text Files|*.txt";
  2.             saveFileDialog1.Title = "Write Data to Text File.";
Now, create a code to show the dialog in the saveFileDialog1 for saving the text file.
  1. saveFileDialog1.ShowDialog();
Initialize the System.IO.StreamWriter having a File variable. This will write data to the saveFileDialog1 filename. We used the StreamWriter for writing a data to this file of saveFileDialog1.
  1.  System.IO.StreamWriter file = new System.IO.StreamWriter(saveFileDialog1.FileName, true);
Make a string variable named data that will hold the inputted text of txtInput.txt.
  1.  string data = txtInput.Text;
Code for writing data to the file.
  1. file.WriteLine(data);
Close your file.
  1.  file.Close();
Full source code:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace write_data_to_textfile_using_CSharp
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.  
  18.         private void button1_Click(object sender, EventArgs e)
  19.         {
  20.             saveFileDialog1.Filter = "Text Files|*.txt";
  21.             saveFileDialog1.Title = "Write Data to Text File.";
  22.             saveFileDialog1.ShowDialog();
  23.    
  24.             System.IO.StreamWriter file = new System.IO.StreamWriter(saveFileDialog1.FileName, true);
  25.  
  26.             string data = txtInput.Text;
  27.  
  28.             file.WriteLine(data);
  29.  
  30.             file.Close();
  31.         }
  32.     }
  33. }
Press F5 to run the program. Input: input Save as sourcodester.txt. Output: output Hope this helps! :) Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment