Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Convert a file to a string
Message
De
25/07/2014 02:36:27
 
 
À
24/07/2014 22:38:47
Information générale
Forum:
ASP.NET
Catégorie:
Autre
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:
01604633
Vues:
44
This message has been marked as a message which has helped to the initial question of the thread.
>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?


It depends

A file is a sequence of bytes and can be any of 3 types

(1) purely binary, eg image

(2) Text. In that case you have to know the encoding ( any of single byte char encodings, any of the double byte chat encodings, utf-8, utf-16 little endian, utf16 big endian, utf-32 little endian, utf32 big endian) before you can convert the byte sequences into chars. Some times there is the BOM at the beginning of the file, sometimes not

(3) Mixed type, eg a database file, a word document, ...
In there there will be binary data (control bytes that indicate a length, the type that follows) and text

If you want to be on the safe side to read the file into memory - and perhaps output it again with another name), treat the file as a sequence of bytes

In that case you would have, either FileToByteArray() or FileToMemoryStream()
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform