Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UTF-8 to ANSI Conversion using C#

I'm a .NET developer and was asked to do an application that converts html files to ANSI in C#.

ANSI is necessary because the converted files will run on a Visual Fox Pro application.

The basic logic is ready the problem is with the conversion itself.

I've tried this code: http://social.msdn.microsoft.com/Forums/pt-BR/026ddda3-9bd1-4502-b445-e2a1cc88345d/convert-file-from-utf8-to-ansi?forum=csharplanguage but when I checked it on editplus the file is still not converted to ANSI and even worst the indentation it's all messed up.

What I should do is to convert a file like editplus does, it preserves the document indentation and can convert any file from UTF8 to ANSI.

The whole point is that I'm working with hundreds of html files, so I can't just do it one by one using a text editor.

How can the conversion be done?

Is there a way to convert it and preserve the indentation like editplus does?

For the special characters like: "ã, ão, é, í..." I'm correcting it before the conversion. Is this the right approach?

like image 477
John Peter Avatar asked Mar 12 '14 17:03

John Peter


1 Answers

Use Default Encoding instead of ASCII:

StreamReader sr = new StreamReader(infile);  
StreamWriter sw = new StreamWriter(outfile, false, Encoding.Default);  

// invoke the ReadToEnd method
sw.WriteLine(sr.ReadToEnd());  

sw.Close();  
sr.Close(); 
like image 121
Mohamed Salemyan Avatar answered Nov 07 '22 18:11

Mohamed Salemyan