登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

樱之花

叶散的时候,你明白欢聚;花谢的时候,你明白青春.

 
 
 

日志

 
 
关于我

分类中“我的实验室”是我在日常工作中的一些知识总结,有些写的比较匆忙,可能大家在阅读时会产生困扰,后期有时间我会重新整理编辑,谢谢大家的到访,您们的支持是我前进的动力!

asp.net 记录页面访问IP并保存到记事本中  

2009-08-10 15:58:41|  分类: .NET/C# |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
File记事本操作

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

  评论这张
 
阅读(2095)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018