First, Google "c# read line of text file". Refer to the MSDN example.
I prefer to use a using statement rather than opening and closing a file. Replace the body of the example's while statement with whatever editing you need to perform on line and then add line to the ListBox.
The following example assumes that System.IO and System.Text appear in a using directive. Replace c:\\gustafson\\test.txt with the name of your text file
public void read_file_into_listbox ( )
{
StringBuilder concatenated = new StringBuilder ( );
int item_count = 0;
string line;
int line_count = 0;
list_box.BeginUpdate ( );
using ( StreamReader file =
new StreamReader (
"c:\\gustafson\\test.txt" ) )
{
concatenated.Append ( "Line " );
while ( ( line = file.ReadLine ( ) ) != null )
{
item_count++;
if ( item_count == 1 )
{
line_count++;
concatenated.AppendFormat (
"{0:d3}: {1} ",
line_count,
line.ToUpper ( ).Trim ( ) );
}
else if ( item_count == 5 )
{
concatenated.AppendFormat (
"{0}",
line.ToUpper ( ).Trim ( ) );
list_box.Items.Add (
concatenated.ToString ( ) );
concatenated.Length = 0;
concatenated.Append ( "Line " );
item_count = 0;
}
else
{
concatenated.AppendFormat (
"{0} ",
line.ToUpper ( ).Trim ( ) );
}
}
if ( item_count > 0 )
{
list_box.Items.Add ( concatenated.ToString ( ) );
}
}
list_box.EndUpdate ( );
}
}
Although string concatenation can occur by using the + operator, more efficient string manipulation can be had by using StringBuilder.
When this code executes, the edited lines of the file will appear in the listbox.
Hope that helps.