Thanks for responding, Peter -
I wish my results were the same as yours. I've attached a couple of files,
ASCII.txt and Unicode.txt. My code is below, FYI.
I've got a form with a textbox on it, and a couple of radiobuttons (encode
or not). To make it simple, I also included a couple of buttons, one for
each file. I display the results of the function below in the textbox.
With encoding, the Unicode file displays fine, and the ASCII file is a
string of unreadable characters. With no encoding, the ASCII file displays
fine, and the Unicode file only displays the first character (=).
I've tried replacing the If-Else-End If block of the code with "rdr =
File.OpenText(rFile)", but my results are the same as the no encoding
results just described.
Note that the Unicode file that I'm trying to read is a log file created by
Microsoft's SQL Server Desktop Engine Setup.exe.
I'd appreciate additional help to solve this problem.
- Jeff
My code:
Public Function ReadFile(ByVal rFile As String) As String
Dim fi As FileInfo
Dim rdr As StreamReader
Try
ReadFile = ""
fi = New FileInfo(rFile)
If Not fi.Exists Then
MessageBox.Show("File Not Found." & ControlChars.CrLf & rFile)
Exit Function
End If
fi = Nothing
If frmMain.optUnicode.Checked Then
rdr = New StreamReader(rFile, System.Text.Encoding.Unicode)
Else
rdr = New StreamReader(rFile)
End If
ReadFile = rdr.ReadToEnd
rdr.Close()
rdr = Nothing
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Function
""Peter Huang"" <v-******@online.microsoft.com> wrote in message
news:gF**************@cpmsftngxa06.phx.gbl...
Hi Jeff,
Based on my test, we do not need to specified the encoding when we read a
file into string, .net framework will handle the issue.
Private Const FILE_NAME As String = "c:\unicode.txt"
Private Const FILE_NAME1 As String = "c:\ascii.txt"
Public Sub Main()
If Not File.Exists(FILE_NAME) Then
Console.WriteLine("{0} does not exist.", FILE_NAME)
Return
End If
Dim sr As StreamReader = File.OpenText(FILE_NAME)
Dim input As String
input = sr.ReadToEnd()
Console.WriteLine(input)
sr.Close()
sr = File.OpenText(FILE_NAME1)
input = sr.ReadToEnd()
Console.WriteLine(input)
sr.Close()
End Sub
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no
rights.