1

i trying to find alternative method to encode UTF-8 to ASCII. Below method works but it takes 1 minutes and 40 second to encode this 486kb file to ASCII

For Each foundFile As String In My.Computer.FileSystem.ReadAllText(My.Settings.TempFile)
        foundFile = foundFile
        My.Computer.FileSystem.WriteAllText(My.Settings.DefaultOutput, foundFile, True, System.Text.Encoding.ASCII)
    Next

if anyone can show me a fastest way than above method, i greatly appreciated.

Thanks

Momento
  • 25
  • 1
  • 8

1 Answers1

1

ReadAllText returns a simple String, so what you are doing is looping over every single character in that string, which will be slow.

Instead, just do

Dim fileText As String = My.Computer.FileSystem.ReadAllText(My.Settings.TempFile)
My.Computer.FileSystem.WriteAllText(My.Settings.DefaultOutput, fileText, True, System.Text.Encoding.ASCII)
nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • Thanks nneonneo, your method provide far more faster way (below than 10sec) than previous one. Thanks again for your answer. I very appreciated it. – Momento Oct 24 '12 at 06:04