記錄處理類
復制代碼 代碼如下:
using System;
using System.IO;
/// <summary>
/// File
/// </summary>
public class File
{
protected string FilePath;
/// <summary>
/// File構造
/// </summary>
/// <param name="filePath">需要操作的文本路徑</param>
public File(string filePath)
{
this.FilePath = filePath;
}
/// <summary>
/// 文本內容寫入
/// </summary>
/// <param name="info">寫入內容</param>
public void FileWrite(string info)
{
try
{
FileInfo file = new FileInfo(FilePath);
if (!file.Exists)
{
using (StreamWriter sw = file.CreateText())
{
sw.WriteLine(info);
}
}
else
{
using (StreamWriter sw = file.AppendText())
{
sw.WriteLine(info);
}
}
}
catch(FileNotFoundException fileCe)
{
throw fileCe;
}
catch (Exception ce)
{
throw ce;
}
}
}
頁面調用代碼
復制代碼 代碼如下:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//判斷當前用戶是否訪問過,只記錄未訪問過的用戶
if (Request.Cookies["IsExitsIP"] == null)
{
//每天一個記事本.txt
string fileName = string.Format("{0}{1}{2}", DateTime.Now.Year.ToString(), DateTime.Now.Month.ToString(), DateTime.Now.Day.ToString());
File file = new File(Server.MapPath("~/test/" + fileName + ".txt"));
file.FileWrite(Request.UserHostName);
//給正在訪問的用戶添加已訪問標記
HttpCookie cokie = new HttpCookie("IsExitsIP");
cokie.Values.Add("ip", Request.UserHostName);
Response.AppendCookie(cokie);
}
}
}
}