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就是空了!
}不知道是什么原因!
有谁能帮忙改改啊!
谢谢!!!!!!!!!!!
{
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就是空了!
}不知道是什么原因!
有谁能帮忙改改啊!
谢谢!!!!!!!!!!!
MessageBox.Show(All);
stream2.WriteLine(All);
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)
进去了没.
所以在第一次运行时正常,第二次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说的,一定要stream2.Close();
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();
经过测试可以删除
?????
if (str.Substring(0, 8) != str1)
{
All += str;
MessageBox.Show(All);
}//测试中数据进入了All;
程序能进入这里,最后哪个MessageBox.Show(All); 不可能显示不出来你是流没有关闭,导致数据丢失了.