Thursday, August 8, 2013

Read file in vb.net


One way

  Dim data As String

        Using sr As New StreamReader("C:\test.txt")
            Do
                data = sr.ReadLine()
                If Not data Is Nothing Then
                    ' do some parsing
                End If
                Console.WriteLine(data)
            Loop Until data Is Nothing
        End Using

2nd way - READ it ALL

Imports System
Imports System.IO

Class ReadALLFile
    Public Shared Sub Main()
        Try 
            Using sr As New StreamReader("TestFile.txt")
                Dim line As String
                line = sr.ReadToEnd()
                Console.WriteLine(line)
            End Using 
        Catch e As Exception         
            Console.WriteLine(e.Message)
        End Try 
    End Sub 
End Class

Read file Character by Character in vb.net


Dim currChar As Char
Dim data As String

  Using sr As New StreamReader("C:\test.txt")
            Do While sr.Peek <> -1
                currChar = Chr(sr.Read)
                data = data & currChar
                Console.WriteLine(data)
            Loop
   End Using

Thursday, August 1, 2013

match ANYTHING using regular expression


Hi,

If you looking a way to match anything in .net using regular expression, the tool you need is:

(?s)

This mean anything including newline as well.

Here is how to use it:
(?s).*    -- comment: Replace angle brackets and mystring word with your word.

HTH.