Windows Forms: How to read text file (*.txt) in C#
By FoxLearn 7/11/2017 8:14:12 PM 12.46K
How to read text (*.txt) file and show data to TextBox in C#
Step 1: Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "ReadTxtFile" and then click OK
Step 2: Design your form as below
Step 3: Add code to button click event handler as below
private async void btnOpen_Click(object sender, EventArgs e) { try { //Open file dialog, allows you to select a txt file using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Text Documents|*.txt", Multiselect = false, ValidateNames = true }) { if (ofd.ShowDialog() == DialogResult.OK) { using (StreamReader sr = new StreamReader(ofd.FileName)) { txtValue.Text = await sr.ReadToEndAsync(); } } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
VIDEO TUTORIALS
- Windows Forms: Drag and Drop controls in C#
- Windows Forms: How to make an Application auto run on Windows startup in C#
- Windows Forms: Wait Form Dialog in C#
- How to create a Lucky Draw games in C#
- Windows Forms: Youtube Search with Paging in C#
- Windows Forms: Text to speech in C#
- Windows Forms: Print Picture Box in C#
- Windows Forms: Advanced Filter DataGridView in C#
Categories
Popular Posts
Visual Studio: Auto increment version
04/01/2024
What is ARM architecture?
04/01/2024