由另一个进程使用 - 流读取器/写入器
本文关键字:读取 另一个 进程 | 更新日期: 2023-09-27 17:55:15
我有一个保存和一个加载函数。 当我在表单上单击"保存"时,我得到"进程无法访问文件'等等',因为它正被另一个进程使用"。
这是我保存和加载函数的代码 -
负荷-
public bool Load()
{
string currentLine;
string[] tokenisedLine;
StreamReader reader;
bool success = false; ;
reader = new StreamReader("Appointments.txt");
try
{
while ((currentLine = reader.ReadLine()) != null)
{
tokenisedLine = currentLine.Split(' ');
DateTime appointmentStartTime = DateTime.Parse(tokenisedLine[0]);
int appointmentLength = int.Parse(tokenisedLine[1]);
string displayableDescription = tokenisedLine[2];
string location = tokenisedLine[3];
appointments.Add(new Appointment(appointmentStartTime, appointmentLength, displayableDescription, location));
}
reader.Close();
success = true;
}
catch(Exception)
{
success = false;
}
return success;
}
救-
public bool Save()
{
string appointStart;
string appointLength;
string appointDescription;
string appointLocation;
bool success = false;
StreamWriter outputFile = new StreamWriter("Appointments.txt");
try
{ // Take appointments out of list and assign their properties to variables
foreach (Appointment appointment in appointments)
{
appointStart = appointment.Start.ToString();
appointLength = appointment.Length.ToString();
appointDescription = appointment.DisplayableDescription;
appointLocation = appointment.Location;
//put variable strings on one line.
string outputLine = appointStart + " "+ appointLength + " "+ appointDescription + " " + appointLocation; ;
// Write line to a txt file and put cursur on a new lne.
outputFile.WriteLine(outputLine);
}
outputFile.Close();
//Close the txt file and return true
success = true;
}
//if unsuccessful catch the exception and return true
catch(Exception)
{
success = false;
}
return success;
}
你绝对应该为你的 StreamReader/StreamWriter 使用using()
块。否则你总是可以关闭你的对象发生或不发生错误
try{
/*...*/
}catch(Exception){
/*...*/
}finally{
outputFile.Close();
}
如果确实没有其他进程访问该文件,则很可能是因为您编写代码的方式。当在任何一个示例中引发异常时,StreamReader
将不会关闭,因此将继续访问该文件。
要解决您的问题,您可以添加一个finally
块或将您的StreamReader
放入using
语句中。
最后:
public bool Load()
{
string currentLine;
string[] tokenisedLine;
StreamReader reader;
bool success = false; ;
reader = new StreamReader("Appointments.txt");
try
{
while ((currentLine = reader.ReadLine()) != null)
{
tokenisedLine = currentLine.Split(' ');
DateTime appointmentStartTime = DateTime.Parse(tokenisedLine[0]);
int appointmentLength = int.Parse(tokenisedLine[1]);
string displayableDescription = tokenisedLine[2];
string location = tokenisedLine[3];
appointments.Add(new Appointment(appointmentStartTime, appointmentLength, displayableDescription, location));
}
success = true;
}
catch(Exception)
{
success = false;
}
finally
{
reader.Close();
}
return success;
}
使用:
public bool Load()
{
string currentLine;
string[] tokenisedLine;
bool success = false; ;
using(var reader = new StreamReader("Appointments.txt"))
{
try
{
while ((currentLine = reader.ReadLine()) != null)
{
tokenisedLine = currentLine.Split(' ');
DateTime appointmentStartTime = DateTime.Parse(tokenisedLine[0]);
int appointmentLength = int.Parse(tokenisedLine[1]);
string displayableDescription = tokenisedLine[2];
string location = tokenisedLine[3];
appointments.Add(new Appointment(appointmentStartTime, appointmentLength, displayableDescription, location));
}
reader.Close();
success = true;
}
catch(Exception)
{
success = false;
}
return success;
}
}
我刚刚完成了Save
方法,但也需要完成Load
方法。