一个简单的网页计数器
今天写了一个最简单的计数器,实现的功能很简单,可以记录总访问数及今日访问数。代码如下:
// ********************************************************
// 网页计数器控件
// Designed by Faib Studio.
// Copyright 2007
// Email faib920@126.com or QQ 55570729
// ********************************************************
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.Drawing.Design;
using System.IO;
using System.Text;
namespace FaibClass.WebControls
{
[ToolboxData("<{0}:Counter runat=server></{0}:Counter>")]
public class Counter : Label
{
私有变量#region 私有变量
private string m_FileName = "";
private string m_ImageUrl = "";
private int m_Length = 5;
private int m_TotalCount, m_TodayCount;
private bool m_OnlyIcon = false;
#endregion
public Counter()
{
}
属性#region 属性
[Browsable(false)]
public override string Text
{
get{return base.Text;}
set{base.Text = value;}
}
[Category("Appearance"),
Description("保存计数的文本文件路径。"),
Editor(typeof(UrlEditor), typeof(UITypeEditor))]
public string FileName
{
get{return m_FileName;}
set{m_FileName = value;}
}
[Category("Appearance"),
Description("使用图象来显示计数,数字使用通配符?代替。"),
Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
public string ImageUrl
{
get{return m_ImageUrl;}
set{m_ImageUrl = value;}
}
[Category("Appearance"),
Description("显示计数的默认长度。"),
DefaultValue(5)]
public int Length
{
get{return m_Length;}
set{m_Length = value;}
}
[Category("Appearance"),
Description("是否只在界面上显示一个图标。"),
DefaultValue(false)]
public bool OnlyIcon
{
get{return m_OnlyIcon;}
set{m_OnlyIcon = value;}
}
//总访问量
[Browsable(false)]
public int TotalCount
{
get{return m_TotalCount;}
set{m_TotalCount = value;}
}
//今日访问量
[Browsable(false)]
public int TodayCount
{
get{return m_TodayCount;}
set{m_TodayCount = value;}
}
#endregion
公共方法#region 公共方法
protected override void OnLoad(EventArgs e)
{
int counter = 0, todaycounter = 0;
DateTime today = DateTime.MinValue;
StringBuilder builder = new StringBuilder();
if(Util.IsDesignMode(Page))//是否设计模式
{
Random rad = new Random();
if(m_ImageUrl.Length == 0)
{
if(m_OnlyIcon)
{
builder.Append("<img src=\"" + m_ImageUrl + "\" align=absmiddle>");
}
else
{
for(int i = 0; i < m_Length; i++)
{
builder.Append(rad.Next(9).ToString());
}
}
}
else
{
if(m_OnlyIcon)
{
builder.Append("<img src=\"" + m_ImageUrl + "\" align=absmiddle>");
}
else
{
for(int i = 0; i < m_Length; i++)
{
builder.Append("<img src=\"" + m_ImageUrl.Replace("?", rad.Next(9).ToString()) + "\" align=absmiddle>");
}
}
}
}
else
{
bool blnExists = File.Exists(Page.Server.MapPath(m_FileName));
if(blnExists)//读出访问量
{
StreamReader str = File.OpenText(Page.Server.MapPath(m_FileName));
try
{
counter = int.Parse(str.ReadLine());
todaycounter = int.Parse(str.ReadLine());
today = DateTime.Parse(str.ReadLine());
}
catch
{
throw new Exception("文件内容为空,请将文件删除");
}
finally
{
str.Close();
}
}
if(Page.Session["__Counter"] == null)
{
counter++;
todaycounter ++;
if(today != DateTime.Today)
{
today = DateTime.Today;
}
}
string s = counter.ToString();
Attributes.Add("title", "总计: " + s + ", 今日: " + todaycounter.ToString());
if(m_OnlyIcon)
{
builder.Append("<img src=\"" + m_ImageUrl + "\" align=absmiddle>");
}
else
{
if(s.Length < m_Length)
{
if(m_ImageUrl.Length == 0)
{
for(int i = 0; i < m_Length - s.Length; i++)
{
builder.Append("0");
}
}
else
{
for(int i = 0; i < m_Length - s.Length; i++)
{
builder.Append("<img src=\"" + m_ImageUrl.Replace("?", "0") + "\" align=absmiddle>");
}
}
}
if(m_ImageUrl.Length == 0)
{
builder.Append(s);
}
else
{
for(int i = 0; i < s.Length; i++)
{
builder.Append("<img src=\"" + m_ImageUrl.Replace("?", s.Substring(i, 1)) + "\" align=absmiddle>");
}
}
}
//将新的访问量写入文件
if(Page.Session["__Counter"] == null)
{
StreamWriter stw = new StreamWriter(Page.Server.MapPath(m_FileName));
stw.WriteLine(counter.ToString());//写入总计
stw.WriteLine(todaycounter.ToString());//写入日期
stw.WriteLine(today.ToShortDateString());//写入今日访问量
stw.Close();
Page.Session["__Counter"] = 1;//标识
}
}
Text = builder.ToString();
m_TotalCount = counter;
m_TodayCount = todaycounter;
base.OnLoad (e);
}
#endregion
}
}
// 网页计数器控件
// Designed by Faib Studio.
// Copyright 2007
// Email faib920@126.com or QQ 55570729
// ********************************************************
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.Drawing.Design;
using System.IO;
using System.Text;
namespace FaibClass.WebControls
{
[ToolboxData("<{0}:Counter runat=server></{0}:Counter>")]
public class Counter : Label
{
私有变量#region 私有变量
private string m_FileName = "";
private string m_ImageUrl = "";
private int m_Length = 5;
private int m_TotalCount, m_TodayCount;
private bool m_OnlyIcon = false;
#endregion
public Counter()
{
}
属性#region 属性
[Browsable(false)]
public override string Text
{
get{return base.Text;}
set{base.Text = value;}
}
[Category("Appearance"),
Description("保存计数的文本文件路径。"),
Editor(typeof(UrlEditor), typeof(UITypeEditor))]
public string FileName
{
get{return m_FileName;}
set{m_FileName = value;}
}
[Category("Appearance"),
Description("使用图象来显示计数,数字使用通配符?代替。"),
Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
public string ImageUrl
{
get{return m_ImageUrl;}
set{m_ImageUrl = value;}
}
[Category("Appearance"),
Description("显示计数的默认长度。"),
DefaultValue(5)]
public int Length
{
get{return m_Length;}
set{m_Length = value;}
}
[Category("Appearance"),
Description("是否只在界面上显示一个图标。"),
DefaultValue(false)]
public bool OnlyIcon
{
get{return m_OnlyIcon;}
set{m_OnlyIcon = value;}
}
//总访问量
[Browsable(false)]
public int TotalCount
{
get{return m_TotalCount;}
set{m_TotalCount = value;}
}
//今日访问量
[Browsable(false)]
public int TodayCount
{
get{return m_TodayCount;}
set{m_TodayCount = value;}
}
#endregion
公共方法#region 公共方法
protected override void OnLoad(EventArgs e)
{
int counter = 0, todaycounter = 0;
DateTime today = DateTime.MinValue;
StringBuilder builder = new StringBuilder();
if(Util.IsDesignMode(Page))//是否设计模式
{
Random rad = new Random();
if(m_ImageUrl.Length == 0)
{
if(m_OnlyIcon)
{
builder.Append("<img src=\"" + m_ImageUrl + "\" align=absmiddle>");
}
else
{
for(int i = 0; i < m_Length; i++)
{
builder.Append(rad.Next(9).ToString());
}
}
}
else
{
if(m_OnlyIcon)
{
builder.Append("<img src=\"" + m_ImageUrl + "\" align=absmiddle>");
}
else
{
for(int i = 0; i < m_Length; i++)
{
builder.Append("<img src=\"" + m_ImageUrl.Replace("?", rad.Next(9).ToString()) + "\" align=absmiddle>");
}
}
}
}
else
{
bool blnExists = File.Exists(Page.Server.MapPath(m_FileName));
if(blnExists)//读出访问量
{
StreamReader str = File.OpenText(Page.Server.MapPath(m_FileName));
try
{
counter = int.Parse(str.ReadLine());
todaycounter = int.Parse(str.ReadLine());
today = DateTime.Parse(str.ReadLine());
}
catch
{
throw new Exception("文件内容为空,请将文件删除");
}
finally
{
str.Close();
}
}
if(Page.Session["__Counter"] == null)
{
counter++;
todaycounter ++;
if(today != DateTime.Today)
{
today = DateTime.Today;
}
}
string s = counter.ToString();
Attributes.Add("title", "总计: " + s + ", 今日: " + todaycounter.ToString());
if(m_OnlyIcon)
{
builder.Append("<img src=\"" + m_ImageUrl + "\" align=absmiddle>");
}
else
{
if(s.Length < m_Length)
{
if(m_ImageUrl.Length == 0)
{
for(int i = 0; i < m_Length - s.Length; i++)
{
builder.Append("0");
}
}
else
{
for(int i = 0; i < m_Length - s.Length; i++)
{
builder.Append("<img src=\"" + m_ImageUrl.Replace("?", "0") + "\" align=absmiddle>");
}
}
}
if(m_ImageUrl.Length == 0)
{
builder.Append(s);
}
else
{
for(int i = 0; i < s.Length; i++)
{
builder.Append("<img src=\"" + m_ImageUrl.Replace("?", s.Substring(i, 1)) + "\" align=absmiddle>");
}
}
}
//将新的访问量写入文件
if(Page.Session["__Counter"] == null)
{
StreamWriter stw = new StreamWriter(Page.Server.MapPath(m_FileName));
stw.WriteLine(counter.ToString());//写入总计
stw.WriteLine(todaycounter.ToString());//写入日期
stw.WriteLine(today.ToShortDateString());//写入今日访问量
stw.Close();
Page.Session["__Counter"] = 1;//标识
}
}
Text = builder.ToString();
m_TotalCount = counter;
m_TodayCount = todaycounter;
base.OnLoad (e);
}
#endregion
}
}