• We just launched and are currently in beta. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

How to Write Data to a Text File using C#

darken_ed

Contributor
D Rep
0
0
0
Rep
0
D Vouches
0
0
0
Vouches
0
Posts
73
Likes
183
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 1000 XP
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:

writedesign.png


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. namespace

    write_data_to_textfile_using_CSharp
  9. {
  10. public

    partial

    class

    Form1 :

    Form
  11. {
  12. public

    Form1(

    )
  13. {
  14. InitializeComponent(

    )

    ;
  15. }

  16. private

    void

    button1_Click(

    object

    sender, EventArgs e)
  17. {
  18. saveFileDialog1.

    Filter

    =

    "Text Files|*.txt"

    ;
  19. saveFileDialog1.

    Title

    =

    "Write Data to Text File."

    ;
  20. saveFileDialog1.

    ShowDialog

    (

    )

    ;

  21. System.IO

    .

    StreamWriter

    file =

    new

    System.IO

    .

    StreamWriter

    (

    saveFileDialog1.

    FileName

    , true

    )

    ;

  22. string

    data =

    txtInput.

    Text

    ;

  23. file.

    WriteLine

    (

    data)

    ;

  24. file.

    Close

    (

    )

    ;
  25. }
  26. }
  27. }

Press F5 to run the program.


Input:

writeinout.png


Save as sourcodester.txt.

Output:

writeoutput.png


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

 

452,497

347,672

347,680

Top