2012-07-15 36 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

相關問題