Solved

vb.net - reading file

Posted on 2011-09-21
5
189 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:XK8ER
  • 2
  • 2
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36577940
I think that you should be using the overloaded StreamReader constructor, with UTF encoding, instead of the default encoder.
0
 
LVL 1

Author Comment

by:XK8ER
ID: 36578222
how can I do that exactly?
0
 
LVL 12

Expert Comment

by:rajapandian_81
ID: 36578474
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

0
 
LVL 1

Author Comment

by:XK8ER
ID: 36578513
now

Copyright ©2000

becomes

Copyright ©2000
0
 
LVL 12

Accepted Solution

by:
rajapandian_81 earned 500 total points
ID: 36578549
ok..

Instead of System.Text.Encoding.Default, try System.Text.Encoding.UTF8.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question