2011-02-27 38 views
0

我有這個應用程序,我使用windowsForm和UserControl來繪製一些圖。完成後,我想保存它們,或者我想打開之前創建的現有文件並繼續處理圖表。所以,我想使用保存並打開對話框文件來保存或打開我的圖表。如何在winForms中保存並打開文件?


編輯:

這是我有:

//save the object to the file 

    public bool ObjectToFile(Object model, string FileName) 
    { 
     try 
     { 
      System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(); 
      System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _BinaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); 
      _BinaryFormatter.Serialize(_MemoryStream, model); 

      byte[] _ByteArray = _MemoryStream.ToArray(); 
      System.IO.FileStream _FileStream = new System.IO.FileStream(FileName,   System.IO.FileMode.Create, System.IO.FileAccess.Write); 
      _FileStream.Write(_ByteArray.ToArray(), 0, _ByteArray.Length); 
      _FileStream.Close(); 

      _MemoryStream.Close(); 
      _MemoryStream.Dispose(); 
      _MemoryStream = null; 
      _ByteArray = null; 

      return true; 

     } 
     catch (Exception _Exception) 
     {   
      Console.WriteLine("Exception caught in process: {0}", _Exception.ToString()); 
     } 
     return false; 

    } 

//load the object from the file 

    public Object FileToObject(string FileName) 
    { 
     try 
     { 
      System.IO.FileStream _FileStream = new System.IO.FileStream(FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); 
      System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream); 
      long _TotalBytes = new System.IO.FileInfo(FileName).Length; 
      byte[] _ByteArray = _BinaryReader.ReadBytes((Int32)_TotalBytes); 
      _FileStream.Close(); 
      _FileStream.Dispose(); 
      _FileStream = null; 
      _BinaryReader.Close(); 

      System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(_ByteArray); 
      System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _BinaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); 
      _MemoryStream.Position = 0; 
      return _BinaryFormatter.Deserialize(_MemoryStream); 

     } 
     catch (Exception _Exception) 
     { 
      Console.WriteLine("Exception caught in process: {0}", _Exception.ToString()); 
     } 
     return null; 
    } 

,現在我想這樣做,但它不工作

public void save() 
    { 
     SaveFileDialog saveFileDialog1 = new SaveFileDialog(); 

     saveFileDialog1.Filter = "txt files (*.txt)|*.txt"; 
     saveFileDialog1.FilterIndex = 2; 
     saveFileDialog1.RestoreDirectory = true; 

     if (saveFileDialog1.ShowDialog() == DialogResult.OK) 
     { 
      if (saveFileDialog1.OpenFile() != null) 
      { 
       ObjectToFile(model, saveFileDialog1.FileName); 
      } 

     } 
    } 

但如果我嘗試沒有fileDialog,我只是用

ObjectToFile(model, "d:\\objects.txt"); 

This works。我想把它保存在我想要的地方並用我自己的名字。

+0

確實如此。下一次嘗試提問。 –

回答

1

查看SaveFileDialogOpenFileDialog類。他們是非常相似的,並且可以像這樣使用:

using(SaveFileDialog sfd = new SaveFileDialog()) { 
    sfd.Filter = "Text Files|*.txt|All Files|*.*"; 
    if(sfd.ShowDialog() != DialogResult.OK) { 
     return; 
    } 

    ObjectToFile(sfd.FileName); 
} 

實際保存文件的機制,很明顯,這個答案的範圍之內。

編輯:我已更新我的回答,以反映您信息中的新信息。

+0

我不明白我接下來要做什麼。 sfd是一個saveFileDialog。我如何把這個sfd保存下來?以及我如何保存?請幫助我 –

+0

我正在假設您知道如何保存/加載您的文件,正如我在答案中所述。由於我對您的數據一無所知,因此我無法對您做出任何進一步的幫助(並且,無論如何,這可能需要一個單獨的問題) –

+0

根據您的新信息,我已經更新了我的答案。你的問題是你打開文件,但沒有關閉它。處理這個問題的最好方法就是試着保存它,你不需要先打開它。 –