国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

記錄游客頁面訪問IP的簡易實現代碼 (asp.net+txt)

2020-01-18 00:58:48
字體:
來源:轉載
供稿:網友
記錄處理類
復制代碼 代碼如下:

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);
}
}
}
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临沧市| 根河市| 襄樊市| 嘉义县| 太仆寺旗| 凌海市| 白朗县| 桑日县| 三都| 南木林县| 芜湖县| 山东省| 安康市| 平南县| 灌阳县| 张家港市| 宁城县| 台江县| 含山县| 遂川县| 林芝县| 昌平区| 蓬莱市| 屯留县| 洪江市| 黄龙县| 铜川市| 遂溪县| 新邵县| 商南县| 来凤县| 南宫市| 基隆市| 盐津县| 南宁市| 清水县| 高尔夫| 津南区| 综艺| 武平县| 拉萨市|