记录游客页面访问IP的简易实现代码 (asp.net+txt)
更新时间:2010年01月15日 17:12:12 作者:
记录游客页面访问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);
}
}
}
}
复制代码 代码如下:
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);
}
}
}
}
相关文章
DotNet OnPreRender(EventArgs e) 事件常用的方法
DotNet OnPreRender(EventArgs e) 事件常用的方法,需要的朋友可以参考下。2011-07-07
asp.net用url重写URLReWriter实现任意二级域名 新
最近有个朋友要做url重写的东西,我帮他弄了弄,回头看当年自己写的那个文章,当时以为自己写的很容易理解.但现在再看却觉得写的不好.而今天百度了一下urlrewriter发现我这个文章竟然排第二.为了方便更多朋友,我再写点东西补充下.2009-11-11
asp.net(C#)使用QRCode生成图片中心加Logo或图像的二维码实例
这篇文章主要介绍了asp.net(C#)使用QRCode生成图片中心加Logo或图像的二维码,结合实例形式详细分析了asp.net基于QRCode生成二维码的具体实现技巧,需要的朋友可以参考下2016-06-06
ABP(现代ASP.NET样板开发框架)系列之二、ABP入门教程详解
ABP是为新的现代Web应用程序使用最佳实践和使用最流行工具的一个起点。可作为一般用途的应用程序的基础框架或项目模板。接下来通过本文给大家详细介绍ABP入门教程,感兴趣的朋友一起看看吧2017-10-10
在.NET中使用Newtonsoft.Json转换,读取,写入的方法介绍
Newtonsoft.Json.JsonConvert类是非微软提供的一个JSON序列化和反序列的开源免费的类库2012-08-08


最新评论