lucamauri
12/22/2013 - 10:07 PM

A collection of three simple class that allow you to read files from disk and from internet and write a stream back to a file. It is a simp

A collection of three simple class that allow you to read files from disk and from internet and write a stream back to a file. It is a simple task for a programmer, but it may comes in hand from time to time to have it ready-to-use.

Imports System.IO
Imports System.Text
Imports System.Net

Module modText
    Function textFromInternet(ByVal address As String) As StringBuilder
        Dim Request As WebRequest
        Dim Response As WebResponse
        Dim Reader As StreamReader
        Dim Line As String
        Dim page As StringBuilder

        Try
            page = New StringBuilder

            Request = WebRequest.Create(address)
            Response = Request.GetResponse
            Reader = New StreamReader(Response.GetResponseStream, System.Text.Encoding.UTF8)

            Line = Reader.ReadLine

            Do
                page.Append(Line & Environment.NewLine)
                Line = Reader.ReadLine
            Loop Until Line Is Nothing

            Return page
        Catch ex As Exception
            Return Nothing
        Finally
            If Reader IsNot Nothing Then
                Reader.Close()
            End If
        End Try
    End Function
    Function textFromFile(ByVal filePath As String) As StringBuilder
        Dim sourceFile As New StreamReader(filePath, System.Text.Encoding.UTF8)
        Dim page As New StringBuilder
        Dim line As String

        Try
            Do
                line = sourceFile.ReadLine
                page.Append(line & Environment.NewLine)
            Loop Until line Is Nothing
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        Finally
            sourceFile.Close()
        End Try
    End Function
    Function textToFile(ByVal destFile As String, ByVal data As String) As Boolean
        Dim outputFile As StreamWriter

        Try
            outputFile = New StreamWriter(destFile, True)
            outputFile.Write(data)

            outputFile.Close()
            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function
End Module