当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 记录游客页面访问IP的简易实现代码 (asp.net+txt)

ASP.NET
asp.net 产生随机颜色实现代码
asp.ent(C#)中判断空字符串的3种方法以及性能分析
asp.net 基于forms验证的目录角色权限的实现
ASP.NET 统计图表控件小结
asp.net 动态引用样式表代码
asp.net 获取IP的相关资料
真正的获取客户端真实IP地址及利弊分析
asp.net(c#)文件下载实现代码
asp.net 不用GridView自带删除功能,删除一行数据
asp.net forms身份验证,避免重复造轮子
asp.net 站点URLRewrite使用小记
asp.net Gridview行绑定事件新体会
asp.net MVC实现简单的上传功能
asp.net web.config加密解密方法
aspx实现的 jquery ui 的 flexgrid demo
ASP.NET Internet安全Forms身份验证方法
asp.net使用for循环实现Datalist的分列显示功能
jQuery AJax调用asp.net webservers的实现代码
ASP.NET 页面刷新和定时跳转代码整理
asp.net GridView控件鼠标移动某行改变背景颜色(方法一)

ASP.NET 中的 记录游客页面访问IP的简易实现代码 (asp.net+txt)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-20   浏览: 162 ::
收藏到网摘: n/a

记录游客页面访问IP的简易实现 (asp.net for notepad) 记录处理类
复制代码 代码如下:

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);
}
}
}
}