Link to home
Start Free TrialLog in
Avatar of XK8ER
XK8ERFlag for United States of America

asked on

vb.net - reading file

Hello there,
I have this function to convert unix text files into windows dos text format.
everything works fine but the issue is that for example I have a text with this in it

|| # Copyright ©2000-2011

after conversion it becomes

|| # Copyright ¿2000-2011

how can I prevent stuff like these from happening?
Private Sub TextUnixToWin(ByVal strFilePath As String)
        Try
            Dim outFile As New List(Of String)
            Using inFile As New IO.StreamReader(strFilePath)
                Try
                    Dim buffer As String = String.Empty
                    Try
                        While Not inFile.EndOfStream
                            buffer = inFile.ReadLine()
                            outFile.Add(buffer)
                        End While
                    Catch ex As Exception
                        Debug.Print("Error performing conversion: {0}", ex.Message)
                        Debug.Print("Press any key to exit...{0}", vbCrLf)
                    End Try
                Catch ex As Exception
                    Debug.Print("Could not open out file: {0}", ex.Message)
                    Debug.Print("Press any key to exit...{0}", vbCrLf)
                End Try
            End Using
            If outFile.Count > 0 Then
                IO.File.WriteAllLines(strFilePath, outFile)
                Debug.Print("done! > " & strFilePath)                
            End If
        Catch ex As Exception
            Debug.Print("Could not open in file: {0}", ex.Message)
            Debug.Print("Press any key to exit...{0}", vbCrLf)
        End Try
    End Sub

Open in new window

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

I think that you should be using the overloaded StreamReader constructor, with UTF encoding, instead of the default encoder.
Avatar of XK8ER

ASKER

how can I do that exactly?
Hi,

Change your line
Using inFile As New IO.StreamReader(strFilePath)
like below:
Using inFile As New IO.StreamReader(strFilePath, System.Text.Encoding.Default, False)

Open in new window

Avatar of XK8ER

ASKER

now

Copyright ©2000

becomes

Copyright ©2000
ASKER CERTIFIED SOLUTION
Avatar of rajapandian_81
rajapandian_81
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial