/// <summary>
/// 遮罩非法字串(如果有出現非法字元,那麼用"***"來替換)
/// </summary>
/// <param name="strText">要檢測的字串</param>
/// <returns>返還一個健康的字元</returns>
public static string CheckKeyword(string strText)
{
IList<string> list = new List<string>(); //具現化一個資料集
string strpath = System.Web.HttpCoNtext.Current.Server.MapPath("function/keyword.txt"); //獲取文字文件路徑
int a =strpath.LastIndexOf("IFSns");
int b =strpath.IndexOf("function");
string m = strpath.Substring(a+5, b - a - 6);
string PathTxt = strpath.Replace(m, ""); //獲取調用這個方法的相對路徑
FileStream fs = new FileStream(PathTxt, FileMode.Open, FileAccess.Read); //打開txt文檔,將資料存到檔流中
StreamReader reader = new StreamReader(fs, Encoding.Default); //檔讀取
string strLine = reader.ReadLine();
 
 
while (strLine!=null&&strLine.Length != 0) //有資料
{
list.Add(strLine.Trim().Replace(" ","")); //如果讀取到的資料有空格,則刪除空格,並且存到string資料集中
strLine = reader.ReadLine(); //每讀取一次,從該行下一行開始繼續讀取
}
fs.Close(); //關閉檔流
foreach (string str in list) //迴圈遍歷檔流
{
if (strText.Contains(str))
{
int lg = str.Length;
string sg = "";
for (int i = 0; i < lg; i++)
{
sg+="*";
}
strText = strText.Replace(str, sg); //如果含有txt文檔中的關鍵字,則替換為"***"
}
}
return strText;
}
 
 
 
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()