"Max" <no****@notvalid.com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
This is what I came up with, adding the latin code now displays chars in
Norway, and the English versions seem to be unaffected. I'm reading them
in using FileStream now, instead of StreamReader.
If anyone has anyway to improve performance, please correct me -- I need
the best performance out of reading these text files, since they are read
in and displayed to the page per each page load.
Dim strFileContents As String
Dim enc As System.Text.Encoding
enc = System.Text.Encoding.GetEncoding(1252)
Dim fs As System.IO.FileStream = New System.IO.FileStream(strFilePath,
FileMode.Open, FileAccess.Read, FileShare.Read)
Dim buffer(4096) As Byte
While fs.Read(buffer, 0, 4096)
strFileContents = strFileContents & enc.GetString(buffer)
End While
Yuck,
All that string concatenation will kill you.
Try this instead, it's short and faster.
Dim strFilePath As String
Dim sr As New StreamReader(strFilePath,
System.Text.Encoding.GetEncoding(1252))
Dim strFileContents As String = sr.ReadToEnd
sr.Close()
For large files you can improve performance a little more if you know how
many bytes per char your encoding has (1252 Latin 1 is a single-byte
encoding), and how large the file is.
Dim fs As System.IO.FileStream = New System.IO.FileStream(strFilePath,
FileMode.Open, FileAccess.Read, FileShare.Read)
Dim bytes As Integer = CInt(fs.Length)
Dim sr As New StreamReader(fs, System.Text.Encoding.GetEncoding(1252),
False, 4096)
Dim sb As New Text.StringBuilder(bytes)
Dim charBuf(4096) As Char
Dim charsRead As Integer = sr.ReadBlock(charBuf, 0, 4096)
Do While charsRead > 0
sb.Append(charBuf, 0, charsRead)
charsRead = sr.ReadBlock(charBuf, 0, 4096)
Loop
sr.Close()
Dim strFileContents As String = sb.ToString
The StringBuilder allows you to pre-allocate the char array for the string.
Then StringBuilder.ToString returns a string which shares that char buffer.
David