用流读取文本文件,记住读取的结束位置,下次直接从该处读取,可是好像没有实现:

realdigua 2004-11-07 01:50:35
private void butopen_Click(object sender, System.EventArgs e)
{
///打开日志。默认500行。
//判断输入字符是不是整数。
Regex r = new Regex("\\D");
Match m = r.Match(tBoxone.Text);
Regex r1 = new Regex("\\D");
Match m1 = r1.Match(tBoxbegincount.Text);
if ((m.Success) ||(m1.Success))
{

string message = "输入的读取的行数不是数字!";
string caption = "错误提示";
MessageBoxButtons buttons = MessageBoxButtons.OK;
MessageBox.Show(this, message, caption, buttons);
}
//判断输入字符是不是整数。
else
{
if(oFDialog.ShowDialog() == DialogResult.OK)
{
lBoxlog.Items.Clear(); //清空显示内容。
string path=oFDialog.FileName;
sr = new StreamReader(path);
//using (sr = new StreamReader(path))
// int nowcount=1;
fmFirelog.ActiveForm.Text=System.Convert.ToString(DateTime.Now);
//记录开始读取的时间。
int bcount=int.Parse(tBoxbegincount.Text);
//从bcount开始读取。
int endcount=bcount+int.Parse(tBoxone.Text);
//读到endcount。
for (int i = 1; i <= endcount; i++)
{
if ((i>=bcount)&&(sr.Peek()!=-1))
{
lBoxlog.Items.Add(sr.ReadLine());
}
else
{
sr.ReadLine();
}
nowcount++;
}

labelnowingcount.Text=System.Convert.ToString(DateTime.Now);
tBoxbegincount.Text=System.Convert.ToString(nowcount);
}
}
}

private void butNext_Click(object sender, System.EventArgs e)
{
// *************** 这里总是直接退出了。难道打开的文件已经变成空了么?
if (sr.Peek()!=-1) { return;}
// *************** 这里总是直接退出了。难道打开的文件已经变成空了么?
Regex r = new Regex("\\D");
Match m = r.Match(tBoxone.Text);
Regex r1 = new Regex("\\D");
Match m1 = r1.Match(tBoxbegincount.Text);
if ((m.Success) ||(m1.Success))
{

string message = "输入的读取的行数不是数字!";
string caption = "错误提示";
MessageBoxButtons buttons = MessageBoxButtons.OK;
MessageBox.Show(this, message, caption, buttons);
return;
}
int endcount=int.Parse(tBoxone.Text); //读到endcount。
for (int i = 1; i <= endcount; i++)
{
lBoxlog.Items.Add(sr.ReadLine());
nowcount++;
}
tBoxbegincount.Text=System.Convert.ToString(nowcount);

}
}
}
...全文
158 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
realdigua 2004-11-07
  • 打赏
  • 举报
回复
谢谢各位,使自己想错了。
BasaraTracy 2004-11-07
  • 打赏
  • 举报
回复
// *************** 这里总是直接退出了。难道打开的文件已经变成空了么?
if (sr.Peek()==-1) { return;}
mobydick 2004-11-07
  • 打赏
  • 举报
回复
brightheroes(闭关|那一剑的风情) ( )
if (sr.Peek()!=-1)
{
return;
}

是不是应该写成

if (sr.Peek()==-1) //<---这里是不是少了一个"="
{
return;
}
xiaoslong 2004-11-07
  • 打赏
  • 举报
回复
帮你顶
520NET 2004-11-07
  • 打赏
  • 举报
回复
LG
zhongwanli 2004-11-07
  • 打赏
  • 举报
回复
顶!
brightheroes 2004-11-07
  • 打赏
  • 举报
回复
if (sr.Peek()!=-1) { return;}

是不是应该写成

if (sr.Peek()=-1) { return;}

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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