类上有Close
orDispose
方法OptionsFile
吗?在退出Save
andLoad
函数之前,您应该确保调用其中一个或另一个。如果你不这样做,那么你可能不得不等到setting_file
实例被垃圾回收之后,文件上的锁才会被释放。如果您不分配大量内存,则可能需要一段时间!
如果OptionsFile.Dispose
存在,则将您的使用包含OptionsFile
在一个using
语句中,该语句将Dispose
在语句末尾自动为您调用,即使抛出异常也是如此。
public void Save(string path , bool Locked , PictureBox pb)
{
string fn;
string t = Path.GetFileNameWithoutExtension(wo_name);
if (File.Exists(path + "\\" + "DATABASE" + "\\" + t + "\\" + wo_name))
{
string f = Path.Combine(path + "\\" + "DATABASE" + "\\" + t + "\\" + wo_name);
File.Delete(f);
fn = path + "\\" + "DATABASE" + "\\" + t + "\\" + wo_name;
}
else
{
fn = path + "\\" + "DATABASE" + "\\" + wo_name + "\\" + wo_name + ".txt";
}
using (OptionsFile setting_file = new OptionsFile(fn))
{
setting_file.SetKey("File Name", fn);
setting_file.SetKey("Version", version);
setting_file.SetKey("Button Lock", Locked.ToString());
setting_file.SetKey("picturebox.Width", pb.Width.ToString());
setting_file.SetKey("picturebox.Height", pb.Height.ToString());
setting_file.SetListFloatKey("Coordinates_X", woc.Point_X);
setting_file.SetListFloatKey("Coordinates_Y", woc.Point_Y);
setting_file.SetListIntKey("ConnectionStart", connectionStart);
setting_file.SetListIntKey("ConnectionEnd", connectionEnd);
}
}
public void Load( string path)
{
string fn = path + "\\" + wo_name;
using (OptionsFile setting_file = new OptionsFile(fn))
{
woc.Point_X = new List<float>();
woc.Point_Y = new List<float>();
woc.Point_X = setting_file.GetListFloatKey("Coordinates_X");
woc.Point_Y = setting_file.GetListFloatKey("Coordinates_Y");
connectionStart = new List<int>();
connectionEnd = new List<int>();
connectionStart = setting_file.GetListIntKey("ConnectionStart");
connectionEnd = setting_file.GetListIntKey("ConnectionEnd");
lockObject = setting_file.GetKey("Button Lock");
}
}
如果OptionsFile.Close
存在,则将您的使用包含OptionsFile
在一个块中,这样即使抛出异常try/finally
,您也可以确保被调用。Close
public void Save(string path , bool Locked , PictureBox pb)
{
string fn;
string t = Path.GetFileNameWithoutExtension(wo_name);
if (File.Exists(path + "\\" + "DATABASE" + "\\" + t + "\\" + wo_name))
{
string f = Path.Combine(path + "\\" + "DATABASE" + "\\" + t + "\\" + wo_name);
File.Delete(f);
fn = path + "\\" + "DATABASE" + "\\" + t + "\\" + wo_name;
}
else
{
fn = path + "\\" + "DATABASE" + "\\" + wo_name + "\\" + wo_name + ".txt";
}
OptionsFile setting_file = null;
try
{
setting_file = new OptionsFile(fn);
setting_file.SetKey("File Name", fn);
setting_file.SetKey("Version", version);
setting_file.SetKey("Button Lock", Locked.ToString());
setting_file.SetKey("picturebox.Width", pb.Width.ToString());
setting_file.SetKey("picturebox.Height", pb.Height.ToString());
setting_file.SetListFloatKey("Coordinates_X", woc.Point_X);
setting_file.SetListFloatKey("Coordinates_Y", woc.Point_Y);
setting_file.SetListIntKey("ConnectionStart", connectionStart);
setting_file.SetListIntKey("ConnectionEnd", connectionEnd);
}
finally
{
if (setting_file != null)
setting_file.Close();
}
}
public void Load( string path)
{
string fn = path + "\\" + wo_name;
OptionsFile setting_file = null;
try
{
setting_file = new OptionsFile(fn);
woc.Point_X = new List<float>();
woc.Point_Y = new List<float>();
woc.Point_X = setting_file.GetListFloatKey("Coordinates_X");
woc.Point_Y = setting_file.GetListFloatKey("Coordinates_Y");
connectionStart = new List<int>();
connectionEnd = new List<int>();
connectionStart = setting_file.GetListIntKey("ConnectionStart");
connectionEnd = setting_file.GetListIntKey("ConnectionEnd");
lockObject = setting_file.GetKey("Button Lock");
}
finally
{
if (setting_file != null)
setting_file.Close();
}
}
如果您没有一个Dispose
或一个Close
方法,那么您需要修改您的OptionsFile
类以实现IDisposable
或拥有一个Close
方法,然后您将关闭或处置任何Stream
基于实例的实例。