如何从文件中读取数据? [英] How to read data from file?
本文介绍了如何从文件中读取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的所有人,
我有问题
我的代码在这里
该代码将数据写入文件
Dear all,
I have a problem
My code is here
this code is write data to file
string tLine = "";
StreamWriter objWriter = new StreamWriter("text.txt",true);
string[] arrTest = new string[9];
arrTest[0] = txtID.Text;
arrTest[1] = txtName.Text;
arrTest[2] = cmbGender.Text;
arrTest[3] = txtPhone.Text;
arrTest[4] = txtPassport.Text;
arrTest[5] = dateTPStartdate.Text;
arrTest[6] = dateTPEndDate.Text;
arrTest[7] = txtAddress.Text;
arrTest[8] = txtDescription.Text;
for (j = 0; j < 9; j++)
{
objWriter.Write(arrTest[j]+" ");
}
objWriter.WriteLine();
objWriter.Close();
然后,当我阅读该文件以显示该文件无法使用的错误时,该文件已被其他进程使用.
此处读取的代码是
Then when I read it to show I got the error that this file cannot use because it uses by other process.
Code read is here
StreamReader objReader = new StreamReader("text.txt");
string textLine = "";
int linecount = 1;
do{
if(linecount % 9 != 0)
{
textLine+= objReader.ReadLine();
linecount++;
}
else
{
lis.SubItems.Add(textLine);
linecount = 1;
textLine = "";
}
} while (objReader.Peek() != -1);
listView1.Items.Add(lis);
我从文件读取以显示在listView中.
请帮我.非常感谢
I read from file to show in listView.
Please help me. Thank so much
推荐答案
在完成处理后处置objWriter-否则文件将保持锁定状态,直到垃圾回收器将其清除为止.
Dispose your objWriter when you have finished with it - otherwise the file will remain locked until the Garbage Collector gets rid of it.
objWriter.WriteLine();
objWriter.Close();
objWriter.Dispose();
objWriter = null; // Not necessary, but prevents you trying to used a disposed item later.
这篇关于如何从文件中读取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文