7

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?

John Peter
  • 73
  • 1
  • 1
  • 5
  • 1
    ["ANSI" is a vague term.](http://stackoverflow.com/questions/701882/what-is-ansi-format) 1. What do you actually mean? 2. Do you know [The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)](http://www.joelonsoftware.com/articles/Unicode.html)? 3. What does your code look like (a link to a forum with various encodings mentioned doesn't give us the full picture)? – Tim S. Mar 12 '14 at 17:57
  • 1
    Use Encoding.Default, not ASCII. It is of course a lossy conversion, you cannot expect an ideal outcome. – Hans Passant Mar 12 '14 at 18:31

1 Answers1

14

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(); 
pholpar
  • 1,725
  • 2
  • 14
  • 23
Mohamed Salemyan
  • 691
  • 2
  • 15
  • 31
  • 1
    I did not foresee that the default was the ANSI encoding. A Huge thanks man. Answering my questions this could help someone in the future: The way showed above don't hurts the indentation and formatting. The best way to convert an utf-8 to ANSI is to first correct the accents if you need to and after it do the properly conversion using Encoding.Default. – John Peter Mar 13 '14 at 11:26
  • It's better to use `Dispose()` method instead of `Close()` method. – Yousha Aleayoub Dec 16 '19 at 09:43
  • This implementation of Close calls the Dispose method, passing a true value.https://learn.microsoft.com/en-us/dotnet/api/system.io.streamreader.close?view=netframework-4.8 – Mohamed Salemyan Jan 13 '20 at 07:06