网站运行一段时间后报错, 只要刷新马上就好, 请教各位大侠这是怎么回事,谢谢!

Aimis 2008-12-01 04:13:27

未能找到文件“D:\Code\Aspx\Test\Aspx\UserCenter\Solution\Count.txt”。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.IO.FileNotFoundException: 未能找到文件“D:\Code\Aspx\Test\Aspx\UserCenter\Solution\Count.txt”。

源错误:

行 11: uint nCount = 0;

行 12: string strFilePath = Server.MapPath("Count.txt");

行 13: System.IO.StreamReader srInfo = System.IO.File.OpenText(strFilePath);

行 14: while (srInfo.Peek() != -1)

行 15: {


源文件: d:\Code\Aspx\Test\Global.asax 行: 13

堆栈跟踪:

[FileNotFoundException: 未能找到文件“D:\Code\Aspx\Test\Aspx\UserCenter\Solution\Count.txt”。]

System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +328

System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) +1038

System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) +115

System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) +88

System.IO.StreamReader..ctor(String path) +112

System.IO.File.OpenText(String path) +32

ASP.global_asax.Application_Start(Object sender, EventArgs e) in d:\Code\Aspx\Test\Global.asax:13





--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:2.0.50727.1433; ASP.NET 版本:2.0.50727.1433



Count.txt这个文件其实是在d:\Code\Aspx\Test\下面,并且只有Global.asax文件中使用,用来网站访问计数的,但登陆后台不用计数,请问有没有什么好的解决方案?
...全文
233 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Aimis 2008-12-01
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 tmxk2002 的回复:]
这样如果不存在是不就重新创建了Count.txt文件了?那里面的数据就太可惜了~
还有一点我比较疑惑,为什么路径会D:\Code\Aspx\Test\Aspx\UserCenter\Solution\Count.txt?
count.txt文件路径明明是在D:\Code\Aspx\Text\下面

-------------------------------------------------------------
如果存在它不会重建啊,怎么会丢失数据呢?你一开始连文件都没有,又怎么可能去计数呢?

你用Server.MapPath,就是取你网站所在的目录了的文件了,这还不好解释,你的文件在D:\Code\Aspx\Text\下,所以就没找到了
[/Quote]
------------------------------------------------------------

Count.txt文件存放在D:\Code\Aspx\Text\下面,
但在后台页打开某页面时,有时会报出这个错误,我不明白为什么路径会D:\Code\Aspx\Test\Aspx\UserCenter\Solution\Count.txt
它要是报出D:\Code\Aspx\Text\Count.txt的错误还可以理解
Aimis 2008-12-01
  • 打赏
  • 举报
回复
这个就是Global.asax文件
Aimis 2008-12-01
  • 打赏
  • 举报
回复
错误文件所有内容:

<%@ Application Language="C#" %>

<script runat="server">

void Application_Start(object sender, EventArgs e)
{

Application["User_Count "] = 0;

uint nCount = 0;
string strFilePath = Server.MapPath("Count.txt");
System.IO.StreamReader srInfo = System.IO.File.OpenText(strFilePath);
while (srInfo.Peek() != -1)
{
string strValue = srInfo.ReadLine();
nCount = UInt32.Parse(strValue);
}

object obj = nCount;
Application["User_Count"] = obj;
srInfo.Close();
}

void Application_End(object sender, EventArgs e)
{
}

void Application_Error(object sender, EventArgs e)
{
}

void Session_Start(object sender, EventArgs e)
{
uint nThisCount = 0;
nThisCount = (uint)Application["User_Count"];
nThisCount = nThisCount + 1;
object obj = nThisCount;

Application.Lock();
Application["User_Count"] = obj;
Application.UnLock();

string strFilePath = Server.MapPath("Count.txt");
System.IO.StreamWriter swInfo = new System.IO.StreamWriter(strFilePath, false);
swInfo.WriteLine(nThisCount);
swInfo.Close();

}

void Session_End(object sender, EventArgs e)
{
uint nThisCount = 0;
nThisCount = (uint)Application["User_Count"];

string strFilePath = Server.MapPath("Count.txt");
System.IO.StreamWriter swInfo = new System.IO.StreamWriter(strFilePath, false);
swInfo.WriteLine(nThisCount);
swInfo.Close();

}

</script>
  • 打赏
  • 举报
回复
这样如果不存在是不就重新创建了Count.txt文件了?那里面的数据就太可惜了~
还有一点我比较疑惑,为什么路径会D:\Code\Aspx\Test\Aspx\UserCenter\Solution\Count.txt?
count.txt文件路径明明是在D:\Code\Aspx\Text\下面

-------------------------------------------------------------
如果存在它不会重建啊,怎么会丢失数据呢?你一开始连文件都没有,又怎么可能去计数呢?

你用Server.MapPath,就是取你网站所在的目录了的文件了,这还不好解释,你的文件在D:\Code\Aspx\Text\下,所以就没找到了

gongsun 2008-12-01
  • 打赏
  • 举报
回复
操作txt文件的时候有问题,可以试下4楼的方法,重起网站看看。缩小错误范围。
yagebu1983 2008-12-01
  • 打赏
  • 举报
回复
你最好判断一下文件是否存在!!
yagebu1983 2008-12-01
  • 打赏
  • 举报
回复
可能是在用到txt文件时出的错误!!
Aimis 2008-12-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 tmxk2002 的回复:]
这个文件不存在,
刷新一下就好的原因是因为Application_Start只有在web网站启动的时候执行的,后面就不会执行了,你再刷新的时候,网站已经起来了,并且没有执行你这段代码,所以就好了。你是不是总是在重启网站后报这个错误?

把这段
if (File.Exists(strFilePath ))
{
File.Create(strFilePath );
}
加到OpenText的前面
[/Quote]

这样如果不存在是不就重新创建了Count.txt文件了?那里面的数据就太可惜了~
还有一点我比较疑惑,为什么路径会D:\Code\Aspx\Test\Aspx\UserCenter\Solution\Count.txt?
count.txt文件路径明明是在D:\Code\Aspx\Text\下面
lxl_sports 2008-12-01
  • 打赏
  • 举报
回复
顶 4楼的 。。。。有道理。。。
但是 如果你只是 为了计数,那么你用session 就可以了啊。。。


并且你存放到数据库中 比较好些。。。
  • 打赏
  • 举报
回复
这个文件不存在,
刷新一下就好的原因是因为Application_Start只有在web网站启动的时候执行的,后面就不会执行了,你再刷新的时候,网站已经起来了,并且没有执行你这段代码,所以就好了。你是不是总是在重启网站后报这个错误?

把这段
if (File.Exists(strFilePath ))
{
File.Create(strFilePath );
}
加到OpenText的前面
Aimis 2008-12-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lihongdian 的回复:]
应该是服务器的原因,你直接用Application记录下来存到数据库.不要存到txt
[/Quote]
有哪些服务器原因会导致这样??
lihongdian 2008-12-01
  • 打赏
  • 举报
回复
应该是服务器的原因,你直接用Application记录下来存到数据库.不要存到txt
风骑士之怒 2008-12-01
  • 打赏
  • 举报
回复
srInfo.Close();

将Application值存到数据库里,根据用户角色不同,定义一个session["Group"]赋值:Member或者Admin,如果session["Group"]==Member,访问量+1 这只是一个思路

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧