using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Configuration; namespace Ant.Frame.Tool { class DeleteKeyword { private static string keyword = ConfigurationManager.AppSettings["keyword"].ToLower(); /// /// 屏蔽非法字符串(如果有出现非法字符,那么用"***"来替换) /// /// 要检测的字符串 /// 返还一个健康的字符 public static string CheckKeyword(string strText) { IList list = new List(); //实例化一个数据集 string strpath = System.Web.HttpContext.Current.Server.MapPath(keyword); //获取文本文档路径 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; } } }