小弟自己写了一个,可以显示出日月总访问量,但在改变时间的时候访问量却不清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
}
}
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
}
}
解决方案 »
- 编个c#程序将一张100元的大钞票,换成等值的20元,10元,5元,1元的四种钞票共10张,每种面值的至少一张
- C# 程序崩溃问题
- 如何动态生成水晶报表的表头。急用!!!
- 跨页面提交问题~~
- .NET打包的安装程序,生成的快捷方式总是调用自动修复安装
- XP下难道没有IIS。
- [求助]httperr1.log 每天都出一些ERROR,请大家帮忙分析一下
- .net2.0开发的程序部署问题。
- 怎么让div层固定在屏幕中间,不随滚动条拖动而不见了。
- 水晶报表控件CrystalReportViewer怎么与数据库连接起来,怎么进行数据绑定?
- 请解释下进制问题:28000H=160*(2的10次方)=160K
- 检测 keyup 事件 请高手指教
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;
}
}
那我需要怎么半??
你说 Loadday() 放在Application_Start()里面,只有在IIS重新启动的时候才会被调用,
但我创建的文件为什么每次都好用呢,他不是放在一个函数里了吗?