小弟自己写了一个,可以显示出日月总访问量,但在改变时间的时候访问量却不清0,只有在重新编译一次之后才能重新开始,,给出源码如下,我将总访问量写在跟目录下的counter.txt里,而日月的访问量写在了counter文件夹下,并且以当天的日期做为文件名,不知道错误出在了哪里,希望高手赐教!!!using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.IO ; namespace DianZiWai 
{
/// <summary>
/// Global 的摘要说明。
/// </summary>
public class Global : System.Web.HttpApplication
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; public Global()
{
InitializeComponent();
}

protected void Application_Start(Object sender, EventArgs e)
{
uint count=0; 
StreamReader srd; 
//取得文件的实际路径 
string file_path=Server.MapPath ("counter.txt"); 
//打开文件进行读取 
srd=File.OpenText (file_path); 
while(srd.Peek ()!=-1) 

string str=srd.ReadLine (); 
count=UInt32.Parse (str); 

object obj=count; 
Application["counter"]=obj; 
srd.Close (); 
Loadmonth();
Loadday();
} private void Loadday()
{
string txtname="counter/"+DateTime.Now.Month+"m"+DateTime.Now.Day+"day.txt";
if(File.Exists(Server.MapPath(txtname)))
{
uint count=0; 
StreamReader srd; 
//取得文件的实际路径 
string file_path=Server.MapPath (txtname); 
//打开文件进行读取 
srd=File.OpenText (file_path); 
while(srd.Peek ()!=-1) 

string str=srd.ReadLine (); 
count=UInt32.Parse (str); 

object obj=count; 
Application["day"]=obj; 
srd.Close (); 
}
else
{
// File.Create(Server.MapPath(txtname));
StreamWriter fs=new StreamWriter(Server.MapPath(txtname),true); 
fs.WriteLine ("0"); 
fs.Close (); 
uint count=0; 
object obj=count; 
Application["day"]=obj; 
}

} private void Loadmonth()
{
string txtname="counter/month"+DateTime.Now.Month+".txt";
if(File.Exists(Server.MapPath(txtname)))
{
uint count=0; 
StreamReader srd; 
//取得文件的实际路径 
string file_path=Server.MapPath (txtname); 
//打开文件进行读取 
srd=File.OpenText (file_path); 
while(srd.Peek ()!=-1) 

string str=srd.ReadLine (); 
count=UInt32.Parse (str); 

object obj=count; 
Application["month"]=obj; 
srd.Close (); 
}
else
{
// File.Create(Server.MapPath(txtname));
StreamWriter fs=new StreamWriter(Server.MapPath(txtname),true); 
fs.WriteLine ("0"); 
fs.Close (); 
uint count=0; 
object obj=count; 
Application["month"]=obj; 
}

}
 
protected void Session_Start(Object sender, EventArgs e)
{
Application.Lock (); 
//数值累加,注意这里使用了装箱(boxing) 
uint jishu=0; 
jishu=(uint)Application["counter"]; 
jishu=jishu+1; 
object obj=jishu; 
Application["counter"]=obj; 
//将数据记录写入文件 
string file_path=Server.MapPath ("counter.txt"); 
StreamWriter fs=new StreamWriter(file_path,false); 
fs.WriteLine (jishu); 
fs.Close (); 
Application.UnLock (); 
month_start();
day_start();
} private void day_start()
{
Application.Lock (); 
//数值累加,注意这里使用了装箱(boxing) 
uint jishu=0; 
jishu=(uint)Application["day"]; 
jishu=jishu+1; 
object obj=jishu; 
Application["day"]=obj; 
//将数据记录写入文件 
string txtname="counter/"+DateTime.Now.Month+"m"+DateTime.Now.Day+"day.txt";
string file_path=Server.MapPath (txtname); 
StreamWriter fs=new StreamWriter(file_path,false); 
fs.WriteLine (jishu); 
fs.Close (); 
Application.UnLock (); 
} private void month_start()
{
Application.Lock (); 
//数值累加,注意这里使用了装箱(boxing) 
uint jishu=0; 
jishu=(uint)Application["month"]; 
jishu=jishu+1; 
object obj=jishu; 
Application["month"]=obj; 
//将数据记录写入文件 
string txtname="counter/month"+DateTime.Now.Month+".txt";
string file_path=Server.MapPath (txtname); 
StreamWriter fs=new StreamWriter(file_path,false); 
fs.WriteLine (jishu); 
fs.Close (); 
Application.UnLock (); 
} protected void Application_BeginRequest(Object sender, EventArgs e)
{ } protected void Application_EndRequest(Object sender, EventArgs e)
{ } protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{ } protected void Application_Error(Object sender, EventArgs e)
{ } protected void Session_End(Object sender, EventArgs e)
{ } protected void Application_End(Object sender, EventArgs e)
{
//装箱 
uint js=0; 
js=(uint)Application["counter"]; 
//object obj=js; 
//Application["counter"]=js; 
//将数据记录写入文件 
string file_path=Server.MapPath ("counter.txt"); 
StreamWriter fs=new StreamWriter(file_path,false); 
fs.WriteLine(js); 
fs.Close (); 
month_end();
day_end();
} private void day_end()
{
uint js=0; 
js=(uint)Application["day"]; 
//object obj=js; 
//Application["counter"]=js; 
//将数据记录写入文件 
string txtname="counter/"+DateTime.Now.Month+"m"+DateTime.Now.Day+"day.txt";
string file_path=Server.MapPath (txtname); 
StreamWriter fs=new StreamWriter(file_path,false); 
fs.WriteLine(js); 
fs.Close (); 
} private void month_end()
{
uint js=0; 
js=(uint)Application["month"]; 
//object obj=js; 
//Application["counter"]=js; 
//将数据记录写入文件 
string txtname="counter/month"+DateTime.Now.Month+".txt";
string file_path=Server.MapPath (txtname); 
StreamWriter fs=new StreamWriter(file_path,false); 
fs.WriteLine(js); 
fs.Close (); 
}

#region Web 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.components = new System.ComponentModel.Container();
}
#endregion
}
}

解决方案 »

  1.   

    to  terryshi总访问量不用清0,只有日,月清0啊,fs.WriteLine ("0"); 
    fs.Close (); 
    uint count=0; 
    object obj=count; 
    Application["day"]=obj; 这不算是清0吗??
    只是编译之后第一次好用!!===========================================================================
    private void Loadday()
    {
    string txtname="counter/"+DateTime.Now.Month+"m"+DateTime.Now.Day+"day.txt";
    if(File.Exists(Server.MapPath(txtname)))
    {
    uint count=0; 
    StreamReader srd; 
    //取得文件的实际路径 
    string file_path=Server.MapPath (txtname); 
    //打开文件进行读取 
    srd=File.OpenText (file_path); 
    while(srd.Peek ()!=-1) 

    string str=srd.ReadLine (); 
    count=UInt32.Parse (str); 

    object obj=count; 
    Application["day"]=obj; 
    srd.Close (); 
    }
    else
    {
    // File.Create(Server.MapPath(txtname));
    StreamWriter fs=new StreamWriter(Server.MapPath(txtname),true); 
    fs.WriteLine ("0"); 
    fs.Close (); 
    uint count=0; 
    object obj=count; 
    Application["day"]=obj; 
    }

    }
      

  2.   

    我不是很明白你的代码,但是你把 Loadday() 放在Application_Start()里面,那么只有在IIS重新启动的时候才会被调用。“改变时间”什么的,我就不理解了
      

  3.   

    to  terryshi
    那我需要怎么半??
    你说 Loadday() 放在Application_Start()里面,只有在IIS重新启动的时候才会被调用,
    但我创建的文件为什么每次都好用呢,他不是放在一个函数里了吗?
      

  4.   

    to  jxufewbt那我写的哪错了吗??