<summary>
Solve the text garbled all kinds of text encoding to UTF-8 text
</summary>
<param name= "FullPath" > Original path </param>
<param name= "Savepath" > Temporary save path </param>
<param name= "strnew" > New file path </param>
<returns> New file path </returns>
public static string GetUtf8 (String fullpath,string savepath,string strnew)
{
Encoding e =getfileencodetype (FullPath);
System.IO.File.Create (Savepath). Close ();
using (StreamReader sr = new StreamReader (FullPath, E, false))
{
String utf8info = String. Empty;
Encoding UTF8 = Encoding.UTF8;
Convert the string into a byte[].
byte[] Unicodebytes = E.getbytes (sr. ReadToEnd ());
Perform. The conversion from one encoding to the other.
byte[] Asciibytes = Encoding.convert (E, UTF8, unicodebytes);
Convert the new byte[] into a char[] and then into a string.
This was a slightly different approach to converting tillustrate
The use of Getcharcount/getchars.
char[] Asciichars = new Char[utf8. GetCharCount (asciibytes, 0, Asciibytes.length)];
UTF8. GetChars (asciibytes, 0, Asciibytes.length, asciichars, 0);
Utf8info = new String (asciichars);
using (StreamWriter SW = new StreamWriter (Savepath, False, Encoding.UTF8))
{
Sw. Write (Utf8info);
}
}
System.IO.File.Delete (FullPath);
System.IO.File.Copy (savepath,strnew,true);
System.IO.File.Delete (Savepath);
return strnew;
}
Method 2
using (FileStream fs = new FileStream (FullPath, FileMode.Open))
{
using (StreamReader sr = new StreamReader (Fs,encoding.default))
{
while (!SR. Endofstream)
{
String sline = Sr. ReadLine ();
StringBuilder B = new StringBuilder ();
B.appendline (sline);
Save to another TXT file
Using (Streamwritersw=system.io.file.appendtext (Savepath))
{
string n = b.tostring ();
Sw. WriteLine (Sline.tostring ());
}
}
}
}
System.IO.File.Delete (FullPath);
string strnewfile = FullPath;
System.IO.File.Copy (Savepath, strNewFile, true);
System.IO.File.Delete (Savepath);
using (StreamReader sr = new StreamReader (Strnewfile,encoding.default, False))
//{
using (StreamWriter SW = new StreamWriter (FullPath, Falsencoding.utf8))
// {
Sw. Write (Server.HTMLEncode (Sr. ReadToEnd ()));
// }
//}
System.IO.File.Delete (strNewFile);
FileInfo filenew = new FileInfo (strnewfile);
Sourcesfile.file_size = Filenew.length;
This.sourcesFileService.Save (Sourcesfile);
}
ASP. Text encoding to UTF-8 text in Chinese characters