解决本地文本文件上传到服务器乱码问题(C#),ANSI转UTF-8编码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace testConsoleApplication1 { class Class2 { public static void Main() { FileStream fs = new FileStream(@"D:\name.xml", FileMode.Open, FileAccess.Read); //D:\name.xml ANSI编码,含中英文 StreamReader sr = new StreamReader(fs,Encoding.Default); //StreamReader sr = new StreamReader(fs);中文会显示乱码,加上Encoding.Default就没有乱码了 FileStream fsw = new FileStream(@"D:\renamed.xml", FileMode.Create, FileAccess.Write); StreamWriter sw = new StreamWriter(fsw, Encoding.UTF8); Char[] charArr = new Char[100]; int count = sr.Read(charArr, 0, 100); while (count != 0) { sw.Write(charArr, 0, count); count = sr.Read(charArr, 0, 100); } sw.Close(); sr.Close(); fsw.Close(); fs.Close(); FileStream fstest = new FileStream(@"D:\renamed.xml", FileMode.Open, FileAccess.Read); StreamReader srtest = new StreamReader(fstest); string rlt = srtest.ReadToEnd(); Console.WriteLine(rlt); } } }