2012-07-15 71 views
1

我试图使用但使用它自己的错误。在这种情况下,如何处理/释放字符串文件?

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)) 
      { 
       using (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 = 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); 


     } 

在此保存功能上面我做:

using (string f = Path.Combine(path + "\\" + "DATABASE" + "\\" + t + "\\" + wo_name)) 
        { 
         File.Delete(f); 
        } 

只是有串F = .... ....路径和Fiel.Delete 我只是说收到使用但在使用即时得到错误:“字符串”:类型using语句中使用必须是隐式转换为“System.IDisposable的”

+0

配置字符串没有意义,字符串只是消耗内存。垃圾收集器已经处理了这个问题。不要在这里使用*。 – 2012-07-15 16:09:20

回答

0

是否有对OptionsFileCloseDispose方法?在退出SaveLoad功能之前,您应该确保打电话给其中一方。如果您不这样做,那么您可能必须等到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(); 
     } 
    } 

如果没有DisposeClose方法,那么你就需要修改OptionsFile类来实现IDisposable或有Close方法,你又接近或任何基于Stream实例的处理。

+0

你能告诉我如何在OptionsFile类中做到这一点吗?编辑它到我的问题。 – user1526380 2012-07-15 06:44:23

+0

@ user1526380感谢发布'OptionsFile'类。在查看'OptionsFile'的代码后,看起来好像不需要'Close'或'Dispose'方法,因为类在两个方法'GetKey'和'SetKey'中执行其所有I/O ,看起来好像流正在被正确关闭(如果没有抛出异常)。另外,您应该使用'using'语句来封装流的使用。 – 2012-07-15 15:51:14

+0

门罗托马斯即时尝试使用,但获得错误使用即时更新我的​​问题再次向您展示如何即时通讯尝试使用例如使用保存功能。 – user1526380 2012-07-15 16:01:31