Today, I am going to show you a small demo of how to read, write and delete the text file content in a Windows Forms ListBox control.
Step1
Create one empty text file and named it as your desired name and add into your project, as shown below.

Create one empty text file and named it as your desired name and add into your project, as shown below.

Step 2
Create a form. In my case, I have added a TextBox, a ListBox, and three Button controls. Double click on buttons to add event handlers for these buttons.
Create a form. In my case, I have added a TextBox, a ListBox, and three Button controls. Double click on buttons to add event handlers for these buttons.
Step3
Add using System.IO; namespace in your C# file.
Add using System.IO; namespace in your C# file.
Now, begin writing code for each button click event handler.
Add the new line to the text file.
ADD button click event handler.
- // Declare the File Information
- FileInfo file = new FileInfo(@ "C:\Users\shekhart\documents\visual studio 2013\study\Demo\Demo\Demo.txt");
- // write the line using streamwriter
- using(StreamWriter sw = file.AppendText()) {
- sw.WriteLine(textBox1.Text);
- }
- // Read the content using Streamreader from text file
- using(StreamReader sr = file.OpenText()) {
- string s = "";
- while ((s = sr.ReadLine()) != null) {
- listBox1.Text = s; // Display the text file content to the list box
- }
- sr.Close();
- }
Delete the line from the text file

Join the conversation! Your thoughts help the community grow.