UTF-8 to ANSI Conversion using C#

烂漫一生 提交于 2020-08-27 05:45:11

问题


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?


回答1:


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(); 


来源:https://stackoverflow.com/questions/22359662/utf-8-to-ansi-conversion-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!