我面临一个问题。在我的程序中,我在桌面上创建了一个文件:
var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
var fullFileName = Path.Combine(desktopFolder, "Report.txt");
var fs = new FileStream(fullFileName, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
[...写入文件...]
sw.WriteLine("Report generated with***.");
sw.Flush();
sw.Close();
sw.Dispose();
fs.Close();
fs.Dispose();
稍后,文件完成后,我再次阅读:
FileInfo fi = new FileInfo(fullFileName);
StreamReader objReader = new StreamReader(fi.OpenRead());
string sLine = "";
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
{
Report.Items.Add(sLine);
}
}
问题是,如果我尝试读取文件两次,我得到一个错误:进程无法访问文件,因为它正在被另一个进程使用。有什么想法可能是错的吗?