今天在將操作記錄寫入文字檔時,遇到寫入文字後的中文變成亂碼,目前找到的解決方案,Encoding.Default 可有效解決這個問題
public static void WriteTxtLog(string strLog, string Path, string FileName)
{
StreamWriter txtwrite = default(StreamWriter);
DirectoryInfo createdir = default(DirectoryInfo);
FileInfo logfile = new FileInfo(Path + "\\" + FileName);
//建log檔
createdir = new DirectoryInfo(logfile.DirectoryName);
if (createdir.Exists == false)
{
createdir.Create();
}
//使用與系統一致的編碼方式
txtwrite = new StreamWriter(logfile.FullName, true, Encoding.Default);
txtwrite.WriteLine(strLog);
txtwrite.Close();
}
沒有留言:
張貼留言