讀寫大文本為防止注入等各種問題,將文本轉換為Unicode或UTF8進行保存
/// <summary>
/// 將文本字符串轉換成帶","號分離的二進制字符串
/// </summary>
/// <param name="strContent">文本字符串</param>
/// <returns>帶,號分離的二進制字符串</returns>
private string strTextTostrBin(string strText)
{
byte[] bytearr=null;
string stringtobin="";
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
bytearr=encoding.GetBytes(strText);
for(int i=0;i<bytearr.Length;i++)
{
stringtobin+=","+bytearr[i].ToString();
}
return stringtobin.Substring(1);
}
/// <summary>
/// 將帶,號分離的二進制字符串轉換成文本字符串
/// </summary>
/// <param name="strBin">帶,號分離的二進制字符串</param>
/// <returns>文本字符串</returns>
private string strBinTostrText(string strBin)
{
string [] bintostr=strBin.Split(',');
Array binArray=Array.CreateInstance(Type.GetType("System.Byte"),bintostr.Length);
for(int i=binArray.GetLowerBound(0);i<=binArray.GetUpperBound(0);i++)
{
binArray.SetValue(byte.Parse(bintostr[i]+""),i);
}
byte[] strtobin=new byte[bintostr.Length];
for(int i=binArray.GetLowerBound(0);i<=binArray.GetUpperBound(0);i++)
{
strtobin[i]=(byte)binArray.GetValue(i);
}
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(strtobin);
}