"Max" <no****@notvali d.com> wrote in message
news:%2******** *******@TK2MSFT NGP14.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.Enc oding
enc = System.Text.Enc oding.GetEncodi ng(1252)
Dim fs As System.IO.FileS tream = New System.IO.FileS tream(strFilePa th,
FileMode.Open, FileAccess.Read , FileShare.Read)
Dim buffer(4096) As Byte
While fs.Read(buffer, 0, 4096)
strFileContents = strFileContents & enc.GetString(b uffer)
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(st rFilePath,
System.Text.Enc oding.GetEncodi ng(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.FileS tream = New System.IO.FileS tream(strFilePa th,
FileMode.Open, FileAccess.Read , FileShare.Read)
Dim bytes As Integer = CInt(fs.Length)
Dim sr As New StreamReader(fs , System.Text.Enc oding.GetEncodi ng(1252),
False, 4096)
Dim sb As New Text.StringBuil der(bytes)
Dim charBuf(4096) As Char
Dim charsRead As Integer = sr.ReadBlock(ch arBuf, 0, 4096)
Do While charsRead > 0
sb.Append(charB uf, 0, charsRead)
charsRead = sr.ReadBlock(ch arBuf, 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.T oString returns a string which shares that char buffer.
David