using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Text; using System.IO; namespace WebApplication1 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnIP_Click(object sender, EventArgs e) { string txtip = txtIP.Text.Trim(); string ip=""; if (txtip != "") { if (txtip.Substring(txtip.Length - 1, 1) != ";") { ip = txtip + ";"; } else { ip = txtip; } string path = Server.MapPath("IP.txt"); if (!File.Exists(path)) { using (File.CreateText(path)) { } } StreamWriter sw = new StreamWriter(path, true);//true表示追加,false表示覆盖 sw.Write(ip); sw.Close(); } else { //填写IP信息 } } protected void btnRead_Click(object sender, EventArgs e) { string path = Server.MapPath("DenyIP.txt"); string sameIP = ""; FileStream fs = new FileStream(path, FileMode.Open); StreamReader sr = new StreamReader(fs, Encoding.Default); string result = null; while (!sr.EndOfStream) { result = sr.ReadLine(); } if(result!="") { string[] arrayIP; if (result.Substring(result.Length - 1, 1) == ";") { arrayIP = result.Substring(0,result.Length-1).Split(';'); } else { arrayIP = result.Split(';'); } for (int i = 0; i < arrayIP.Length; i++) { for (int j = 0; j < arrayIP.Length; j++) { if (i != j) { if (arrayIP[i] == arrayIP[j]) { sameIP += arrayIP[i] + ";"; } } } } } txtIP.Text = sameIP; sr.Close(); fs.Close(); } } }