Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Convert a file to a string
Message
De
24/07/2014 22:38:47
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Titre:
Convert a file to a string
Versions des environnements
Environment:
VB 9.0
OS:
Windows 8.1
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01604630
Message ID:
01604630
Vues:
43
I have those two methods used to convert a file to a string:
    ' File to string
    Public Function FileToString() As Boolean
        Dim lcFile As String = ""
        Dim llSuccess As Boolean = False
        Dim lnCounter As Integer = 0
        Dim lnDelay As Integer = 500
        Dim lnRetry As Integer = 8
        Dim loFile As IO.StreamReader

        ' Reset the values
        cMessage = ""
        cString = ""

        ' Initialization
        lcFile = Trim(cFile)

        For lnCounter = 1 To lnRetry

            Try
                loFile = New IO.StreamReader(lcFile, True)
                cString = loFile.ReadToEnd()
                loFile.Close()
                llSuccess = True
                Exit For

            Catch loError As Exception

                ' If we have not reached the maximum tries of 8, we retry
                If lnCounter < 8 Then

                    ' Wait for a timeout before retrying
                    System.Threading.Thread.Sleep(lnDelay)

                    Continue For
                End If

                cMessage = loError.Message
            End Try

        Next

        ' Reset the values
        cFile = ""

        Return llSuccess
    End Function

    ' Same as FileToString() but for binary files
    ' This is used when you want to take a JPG, for example, and encode it as Base64 into a XSL
    Public Function FileToStringBinary() As Boolean
        Dim lcFile As String = ""
        Dim llSuccess As Boolean = False
        Dim lnCounter As Integer = 0
        Dim lnDelay As Integer = 500
        Dim lnRetry As Integer = 8
        Dim loFile As IO.StreamReader

        ' Reset the values
        cMessage = ""
        cString = ""

        ' Initialization
        lcFile = Trim(cFile)

        For lnCounter = 1 To lnRetry

            Try
                loFile = New IO.StreamReader(lcFile, System.Text.Encoding.Default)
                cString = loFile.ReadToEnd()
                loFile.Close()
                llSuccess = True
                Exit For

            Catch loError As Exception

                ' If we have not reached the maximum tries of 8, we retry
                If lnCounter < 8 Then

                    ' Wait for a timeout before retrying
                    System.Threading.Thread.Sleep(lnDelay)

                    Continue For
                End If

                cMessage = loError.Message
            End Try

        Next

        ' Reset the values
        cFile = ""

        Return llSuccess
    End Function
The only difference is in the StreamReader() line.

The first one was initially built. Then, I built one to be compliant with files such as JPG, so to grab the content, and use it.

You can see StreamReader() has an overload. It can accept True as is, or an encoding.

I am wondering if the second one would be good enough to always use instead of having two.

Any comment on this?
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform