我有两个butten,分别是buttonOK.,buttonOK7
buttonOK是用于查找我保存在test.txt 文本中的数据
buttonOK7是用于把test.txt 中于textBName相同的数据删除出的!
我的代码是:
private void buttonOK_Click(object sender, System.EventArgs e)
{
string str;
StreamReader stream = new StreamReader(@"f:\Test.txt");
string str1 = this.textBName.Text;
while (str1.Length < 8)
str1 = str1 + " ";
if (str1.Length > 8)
str1 = str1.Substring(0, 8);
for(;;)
{
str = stream.ReadLine();
try
{
if (str1 == str.Substring(0, 8))
{
textBoxName.Text = str.Substring(0, 8);
textBoxAge.Text = str.Substring(8, 3);
if(str.Substring(8+3,5)=="男")
this.radioButBoy.Checked =true;
else
this.radioButGirl.Checked =true;
textBoxID.Text = str.Substring(8 + 3 + 5, 20);
textBoxEmail.Text = str.Substring(36, 25);
textBoxTelphone.Text = str.Substring(36 + 25, 15); textBoxPassWord.Text = str.Substring(36 + 25 + 15, 15);
textBoxAdress.Text = str.Substring(36 + 25 + 30, 30);
break;
}
}
catch(NullReferenceException )
{MessageBox.Show ("没你有查找到你的输入的用户!");break;} }
} private void buttonOK7_Click(object sender, EventArgs e)
{
string str, All=null ;
StreamReader stream1 = new StreamReader(@"f:\Test.txt");
while (stream1.ReadLine() != null)
{
str = stream1.ReadLine();
if (str != textBName.Text)
All += str;
else if (str == textBName.Text)
MessageBox.Show("成功!");
}
StreamWriter stream2 = new StreamWriter(@"f:\Test.txt");
stream2.WriteLine(All); }
执行后遇到问题:未处理的“System.IO.IOException”类型的异常出现
未处理 System.IO.IOException
Message="文件“f:\\Test.txt”正由另一进程使用,因此该进程无法访问该文件。"
{
string str;
StreamReader stream = new StreamReader(@"f:\Test.txt");
//你的代码
stream.Close();
}
private void buttonOK7_Click(object sender, EventArgs e)
{
string str, All=null ;
StreamReader stream1 = new StreamReader(@"f:\Test.txt");
while (stream1.ReadLine() != null)
{
str = stream1.ReadLine();
if (str != textBName.Text)
All += str;
else if (str == textBName.Text)
MessageBox.Show("成功!");
}
stream1.Close();
StreamWriter stream2 = new StreamWriter(@"f:\Test.txt");
stream2.WriteLine(All);
stream2.Close(); }