Transfer data between two or more sub-systems is an integration process of bringing data together into one functional systems. It is important to reduce redundant data entry. There is a wide variety of methods when it comes to read text file in C#. We will use ReadAllLines() method to read all lines of the file into a string array.
The tutorial below shows you how to read data from text files with delimiter in Visual Studio C#.net.
As example, we use the text files with “,” delimiter to specify the boundary between separate of data fields. This file contains two data fields (days number and days name).
The below is the user interface with File name (Label), File path(Text Box), Find (Button), Read (Button), Result (Label) and ofdBrowser (OpenFileDialog).
1. Add using System.IO system namespace into the code
2. Add code in the Find Button with click Event to pop up Windows dialog box (OpenFileDialog) for browsing and selecting files. Once selected, place the file name and path into txtFileName(Text Box).
3. Add code in Read Button with click Event for read all lines of the file into a string array, split the the data in line with “,” delimiter and display the result.
string[] words = a.Split(new char[] { ',' }, StringSplitOptions.None)
4. The result will show as below: