DeleteKeyword.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Configuration;
  7. namespace Ant.Frame.Tool
  8. {
  9. class DeleteKeyword
  10. {
  11. private static string keyword = ConfigurationManager.AppSettings["keyword"].ToLower();
  12. /// <summary>
  13. /// 屏蔽非法字符串(如果有出现非法字符,那么用"***"来替换)
  14. /// </summary>
  15. /// <param name="strText">要检测的字符串</param>
  16. /// <returns>返还一个健康的字符</returns>
  17. public static string CheckKeyword(string strText)
  18. {
  19. IList<string> list = new List<string>(); //实例化一个数据集
  20. string strpath = System.Web.HttpContext.Current.Server.MapPath(keyword); //获取文本文档路径
  21. int a = strpath.LastIndexOf("IFSns");
  22. int b = strpath.IndexOf("function");
  23. string m = strpath.Substring(a + 5, b - a - 6);
  24. string PathTxt = strpath.Replace(m, ""); //获取调用这个方法的相对路径
  25. FileStream fs = new FileStream(PathTxt, FileMode.Open, FileAccess.Read); //打开txt文档,将数据存到文件流中
  26. StreamReader reader = new StreamReader(fs, Encoding.Default); //文件读取
  27. string strLine = reader.ReadLine();
  28. while (strLine != null && strLine.Length != 0) //有数据
  29. {
  30. list.Add(strLine.Trim().Replace(" ", "")); //如果读取到的数据有空格,则删除空格,并且存到string数据集中
  31. strLine = reader.ReadLine(); //每读取一次,从该行下一行开始继续读取
  32. }
  33. fs.Close(); //关闭文件流
  34. foreach (string str in list) //循环遍历文件流
  35. {
  36. if (strText.Contains(str))
  37. {
  38. int lg = str.Length;
  39. string sg = "";
  40. for (int i = 0; i < lg; i++)
  41. {
  42. sg += "*";
  43. }
  44. strText = strText.Replace(str, sg); //如果含有txt文档中的关键字,则替换为"***"
  45. }
  46. }
  47. return strText;
  48. }
  49. }
  50. }