c# 删除处理!

perfect_love 2008-07-04 03:44:16
private void buttonOK5_Click(object sender, EventArgs e) //删除
{
string str, All ;
StreamReader stream1 = new StreamReader(@"f:\Test.txt", true);
string str1 = this.textBName.Text;
while (str1.Length < 8)
str1 = str1 + " ";
if (str1.Length > 8)
str1 = str1.Substring(0, 8);
while ((str = stream1.ReadLine()) != null)
{
//str = stream1.ReadLine();
if (str.Substring(0, 8) != str1)
{
All += str;
MessageBox.Show(All); //测试中数据进入了All; }
else if (str.Substring(0, 8) == str1)
MessageBox.Show("成功!");
}
stream1.Close();
StreamWriter stream2 = new StreamWriter(@"f:\Test.txt");
stream2.WriteLine(All);
MessageBox.Show(All); //可是在这里All就是空了!
}

不知道是什么原因!
有谁能帮忙改改啊!
谢谢!!!!!!!!!!!
...全文
169 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
我姓区不姓区 2008-07-04
  • 打赏
  • 举报
回复
没有关闭流
perfect_love 2008-07-04
  • 打赏
  • 举报
回复
就是哈!谢谢13楼哈!
soaringbird 2008-07-04
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 perfect_love 的回复:]
在最后加  stream2.Close();  All里是不为空了,不过不管我有多少的数据,就第一条记录回被保存!
?????
[/Quote]

你的程序把所有的内容连接成一行了
chinaicm 2008-07-04
  • 打赏
  • 举报
回复
LZ,你那程序我测试了,如果
if (str.Substring(0, 8) != str1)
{
All += str;
MessageBox.Show(All);
}//测试中数据进入了All;
程序能进入这里,最后哪个MessageBox.Show(All); 不可能显示不出来

你是流没有关闭,导致数据丢失了.
perfect_love 2008-07-04
  • 打赏
  • 举报
回复
在最后加 stream2.Close(); All里是不为空了,不过不管我有多少的数据,就第一条记录回被保存!
?????
yuxianye1 2008-07-04
  • 打赏
  • 举报
回复
string str, All="" ; 
StreamReader stream1 = new StreamReader(@"f:\Test.txt", true);
string str1 = "11111111";
while (str1.Length < 8)
str1 = str1 + " ";
if (str1.Length > 8)
str1 = str1.Substring(0, 8);

while ((str = stream1.ReadLine()) != null)
{
//str = stream1.ReadLine();
if (str.Substring(0, 8) != str1)
{
All += str;
MessageBox.Show(All); //测试中数据进入了All;
}
else if (str.Substring(0, 8) == str1)
MessageBox.Show("成功!");
}
stream1.Close();

StreamWriter stream2 = new StreamWriter(@"f:\Test.txt");
stream2.WriteLine(All);
MessageBox.Show(All); //可是在这里All就是空了!
stream2.Close();



经过测试可以删除
zzyhuian06142 2008-07-04
  • 打赏
  • 举报
回复
这个是测试过的
像soaringbird说的,一定要stream2.Close();
zzyhuian06142 2008-07-04
  • 打赏
  • 举报
回复
string str="", All="" ;
StreamReader stream1 = new StreamReader(@"c:\CSDN.txt", true);
string str1 = "KevinTex";
// while (str1.Length < 8)
// str1 = str1 + " ";
// if (str1.Length > 8)
// str1 = str1.Substring(0, 8);
while ((str = stream1.ReadLine()) != null)
{
//str = stream1.ReadLine();
if (str.Substring(0, 8) != str1)
{
All += str;
MessageBox.Show(All);
}//测试中数据进入了All;
else if (str.Substring(0, 8) == str1)
MessageBox.Show("成功!");
}
stream1.Close();
StreamWriter stream2 = new StreamWriter(@"c:\CSDN.txt");
stream2.WriteLine(All);
stream2.Close();
MessageBox.Show(All); //可是在这里All就是空了!
soaringbird 2008-07-04
  • 打赏
  • 举报
回复
最后写入后没有stream2.Close();所以数据就丢了。
所以在第一次运行时正常,第二次all就是空的了
hopewoo 2008-07-04
  • 打赏
  • 举报
回复

string str, All ;
All = "";
StreamReader stream1 = new StreamReader(@"f:\Test.txt", true);
string str1 = "gfdgfdgfdgdf";
while (str1.Length < 8)
str1 = str1 + " ";
if (str1.Length > 8)
str1 = str1.Substring(0, 8);
while ((str = stream1.ReadLine()) != null)
{
//str = stream1.ReadLine();
if (str.Substring(0, 8) != str1)
{
All += str;
MessageBox.Show(All);
}//测试中数据进入了All; }
else
{
if (str.Substring(0, 8) == str1)
MessageBox.Show("成功!");
}
}
stream1.Close();
StreamWriter stream2 = new StreamWriter(@"f:\Test.txt");
stream2.WriteLine(All);
MessageBox.Show(All); //可是在这里All就是空了!


如果test.txt里面有内容的话,这段代码是没问题的.关键看你
while ((str = stream1.ReadLine()) != null)
进去了没.
xhan2000 2008-07-04
  • 打赏
  • 举报
回复
是不是这里
stream2.WriteLine(All);
yuxianye1 2008-07-04
  • 打赏
  • 举报
回复
stream2.WriteLine(All); //这里有么?
MessageBox.Show(All);
soaringbird 2008-07-04
  • 打赏
  • 举报
回复
string str, All = "";
zzyhuian06142 2008-07-04
  • 打赏
  • 举报
回复
建议设置断点调试一下,看ALL在哪边为空了
zhchg6666 2008-07-04
  • 打赏
  • 举报
回复
在最后执行stream1.Close(); 试下
zzyhuian06142 2008-07-04
  • 打赏
  • 举报
回复
看错了,呵呵
zzyhuian06142 2008-07-04
  • 打赏
  • 举报
回复
StreamReader里面不是保存写入的吧

110,825

社区成员

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

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

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