.net读取和写入中英文字符串
我现在在做一个从文本框(Web页面)中获得string 然后将其写入一个txt文件中。要保持可以按格式写入中英文和符号。我写了个 但中文显示是???怎么解决。谢谢
string Num = txt1.Text.ToString();
string Body = txt2.Text.ToString();
char[] charData = Body.ToCharArray();
byte[] byData=new byte[charData.Length];
try
{
FileStream aFile = new FileStream(@ "E:\Temp.txt ",FileMode.Create);
Encoder ff = Encoding.UTF8.GetEncoder();
ff.GetBytes(charData,0,charData.Length,byData,0,true);
aFile.Seek(0,SeekOrigin.Begin);
aFile.Write(byData,0,byData.Length);
aFile.Close();
}
catch(IOException ex)
{
lblShow.Text = ex.ToString();
return;
}
------解决方案--------------------Encoder ff = Encoding.UTF8.GetEncoder();
==========================================> > > >
Encoder ff = System.Text.Encoding.BigEndianUnicode.GetEncoder();
------解决方案--------------------/// <summary>
/// 文本记录日志,以检数据是否正确
/// </summary>
/// <param name= "str0 "> 存储文件名 </param>
/// <param name= "str1 "> 需要记录的值 </param>
/// <param name= "str2 "> 需要记录的值 </param>
/// <param name= "str3 "> 需要记录的值 </param>
public void log(string str0,string str1 ,string str2,string str3)
{
StreamWriter strw;
string filepa= " "+str0+ ".txt ";
string mypath=HttpContext.Current.Server.MapPath(filepa);
strw=File.AppendText(mypath);
strw.WriteLine( "---------------------- ");
strw.WriteLine( "日期时间: "+DateTime.Now.ToString());
strw.WriteLine(str1);
strw.WriteLine(str2);
strw.WriteLine(str3);
strw.WriteLine( "---------------------- ");
strw.WriteLine();
strw.Close();
}
*****************************************************************************