我有一个保存和加载功能。当我点击“保存”时在我的表格上,我得到一个'该过程无法访问该文件' blah blah'因为它正被另一个进程使用'。
这是我的保存和加载功能代码 -
加载 -
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;
}
答案 0 :(得分:0)
您绝对应该使用using()
阻止StreamReader / StreamWriter。否则你可以随时关闭你的对象或不是错误
try{
/*...*/
}catch(Exception){
/*...*/
}finally{
outputFile.Close();
}
答案 1 :(得分:0)
如果确实没有其他进程访问该文件,很可能是因为您编写代码的方式。如果您的任何一个示例中都引发了异常,则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
方法也需要完成。