Tanmay Tanmay - 5 months ago 25
Vb.net Question

How to skip first line while reading the csv in vb

Try

If Not MyCache.GetInstance.AzureCacheOn Then
reader = New IO.StreamReader(FilePathUtility.GetInstance.MapPath(objUrl))
Else

request = System.Net.HttpWebRequest.Create(objUrl)
response = DirectCast(request.GetResponse, System.Net.HttpWebResponse)
strm = DirectCast(response.GetResponseStream, IO.Stream)
reader = New IO.StreamReader(strm)
End If

Dim list = New List(Of String)()

Using reader
Dim line As String
While (InlineAssignHelper(line, reader.ReadLine())) IsNot Nothing
list.Add(line)
End While
End Using


lines = list.ToArray()



Catch ex As Exception
Throw ex
End Try



Private Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
target = value
Return value
End Function


I have this code snippet which is working fine but now in csv file i need to add some information in first line so i need to skip the first line how I can do that please help screenshot of csv is attachedFirst Line skip

Answer

Call read line before while, also check if the line is not blank and then only go to loop. This will skip your first line.

Code something like :

Using reader
    Dim line As String
    // if reader.ReadLine() is not empty
         While (InlineAssignHelper(line, reader.ReadLine())) IsNot Nothing
             list.Add(line)
         End While
    // end if
End Using

To skip nth line (2nd in your case) use :

     Using reader
        Dim line As String
        // count = 0, n = 1 // 0 = first line, 1 = 2nd line etc
        While (InlineAssignHelper(line, reader.ReadLine())) IsNot Nothing
             // if(count++ != n)                
                 list.Add(line)
             // end if
        End While
    End Using
Comments