Hi mohal92,
Check this example. Now please take its reference and correct your code.
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
string header = "", trailer = "";
int lineCount = 0;
List<string> lines = System.IO.File.ReadAllLines(Server.MapPath("~/Test.txt")).Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
if (lines.Count > 0)
{
if (lines.Count == 1)
{
header = lines[0];
}
else if (lines.Count == 2)
{
header = lines[0];
trailer = lines[1];
}
else if (lines.Count > 2)
{
header = lines[0];
trailer = lines[lines.Count - 1];
lineCount = lines.Count - 2;
}
}
Response.Write(string.Format("Header : {0}<br/>Trailer : {1}<br/>Count : {2}", header, trailer, lineCount));
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim header As String = "", trailer As String = ""
Dim lineCount As Integer = 0
Dim lines As List(Of String) = System.IO.File.ReadAllLines(Server.MapPath("~/Test.txt")).Where(Function(x) Not String.IsNullOrWhiteSpace(x)).ToList()
If lines.Count > 0 Then
If lines.Count = 1 Then
header = lines(0)
ElseIf lines.Count = 2 Then
header = lines(0)
trailer = lines(1)
ElseIf lines.Count > 2 Then
header = lines(0)
trailer = lines(lines.Count - 1)
lineCount = lines.Count - 2
End If
End If
Response.Write(String.Format("Header : {0}<br/>Trailer : {1}<br/>Count : {2}", header, trailer, lineCount))
End Sub
Output
Header : 23
Trailer : 8000002
Count : 2