2017-02-25 58 views
0

我使用BinayFormatter序列化值 我想我的控件(comboBox和textBox)自動保存,無需點擊按鈕或關閉表單。 但隨着每一個變化自動。 你能幫我嗎,我發佈的代碼(例如在網上找到,我必須適應)。 代碼肯定含有其他錯誤,如果你已經看到他們 謝謝你對我的幫助C#Form BinaryFormater序列化自動保存

Look my Form demo img

類:

using System; 

    namespace XML_Serialize_Binary 
    { 
     [Serializable] 
     class CNM_Serialize_Settings 
     { 

      public string Selected_Lang; 
      public string Selected_Folder; 

      public CNM_Serialize_Settings(string Lang, string Folder) 
      { 
       Selected_Lang = Lang; 
       Selected_Folder = Folder; 
      } 
     } 
    } 

表:

using System; 
using System.Drawing; 
using System.IO; 
using System.Runtime.Serialization.Formatters.Binary; 
using System.Windows.Forms; 

namespace XML_Serialize_Binary 
{ 
    public partial class Binarytest : Form 
    { 

     BinaryFormatter Binary = new BinaryFormatter(); 
     FileStream filestream1, filestream2; 

     public Binarytest() 
     { 
      InitializeComponent(); 
     } 

     private void Binarytest_Load(object sender, EventArgs e) 
     { 
      filestream2 = new FileStream(@".\setting.bin", FileMode.Open, FileAccess.Read); 
      CNM_Serialize_Settings Load_Settings = (CNM_Serialize_Settings)Binary.Deserialize(filestream2); 
      comboBox1.DropDownStyle = ComboBoxStyle.DropDownList; 
      comboBox1.Font = new Font("SegoeUI", 12); 
      comboBox1.Name = "comboBox1"; 
      comboBox1.Items.Add("american"); 
      comboBox1.Items.Add("french"); 
      comboBox1.Items.Add("german"); 
      comboBox1.Items.Add("russian"); 
      comboBox1.SelectedItem = Load_Settings.Selected_Lang; 
      Select_textBox.Text = Load_Settings.Selected_Folder; 

      filestream2.Flush(); 
      filestream2.Close(); 
     } 

     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
     } 

     private void Select_button_Click(object sender, EventArgs e) 
     { 
      folderBrowserDialog1.Description = "Language:"; 
      folderBrowserDialog1.SelectedPath = Select_textBox.Text; 
      if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) 
      { 
       Select_textBox.Text = folderBrowserDialog1.SelectedPath; 
      } 
     } 

     private void Save_button_Click(object sender, EventArgs e) 
     { 
      string Selected_Lang = comboBox1.SelectedItem.ToString(); 
      string Selected_Folder = Select_textBox.Text; 
      CNM_Serialize_Settings Save_Settings = new CNM_Serialize_Settings(Selected_Lang, Selected_Folder); 
      filestream1 = new FileStream(@".\setting.bin", FileMode.Create, FileAccess.Write); 
      Binary.Serialize(filestream1, Save_Settings); 
      filestream1.Flush(); 
      filestream1.Close(); 
     } 
    } 
} 

回答

0
using System; 
using System.Drawing; 
using System.IO; 
using System.Runtime.Serialization.Formatters.Binary; 
using System.Windows.Forms; 

namespace XML_Serialize_Binary 
{ 
public partial class Binarytest : Form 
{ 

    BinaryFormatter Binary = new BinaryFormatter(); 
    FileStream filestream1, filestream2; 

    public Binarytest() 
    { 
     InitializeComponent(); 
    } 

    private void Binarytest_Load(object sender, EventArgs e) 
    { 
     filestream2 = new FileStream(@".\setting.bin", FileMode.Open, FileAccess.Read); 
     CNM_Serialize_Settings Load_Settings = (CNM_Serialize_Settings)Binary.Deserialize(filestream2); 
     comboBox1.DropDownStyle = ComboBoxStyle.DropDownList; 
     comboBox1.Font = new Font("SegoeUI", 12); 
     comboBox1.Name = "comboBox1"; 
     comboBox1.Items.Add("american"); 
     comboBox1.Items.Add("french"); 
     comboBox1.Items.Add("german"); 
     comboBox1.Items.Add("russian"); 
     comboBox1.SelectedItem = Load_Settings.Selected_Lang; 
     Select_textBox.Text = Load_Settings.Selected_Folder; 

     filestream2.Flush(); 
     filestream2.Close(); 
    } 

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
save(); 
    } 

    private void Select_button_Click(object sender, EventArgs e) 
    { 
     folderBrowserDialog1.Description = "Language:"; 
     folderBrowserDialog1.SelectedPath = Select_textBox.Text; 
     if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) 
     { 
      Select_textBox.Text = folderBrowserDialog1.SelectedPath; 
save(); 
     } 
    } 

    private void save() 
    { 
     string Selected_Lang = comboBox1.SelectedItem.ToString(); 
     string Selected_Folder = Select_textBox.Text; 
     CNM_Serialize_Settings Save_Settings = new CNM_Serialize_Settings(Selected_Lang, Selected_Folder); 
     filestream1 = new FileStream(@".\setting.bin", FileMode.Create, FileAccess.Write); 
     Binary.Serialize(filestream1, Save_Settings); 
     filestream1.Flush(); 
     filestream1.Close(); 
    } 
} 
} 
+0

感謝Dhanesh的回答,我使用它時出現問題調試告訴我: 進程無法訪問'setting.bin'文件,因爲它正在被另一個進程使用。 – Carsinka

+0

這是因爲程序打開了setting.bin文件。嘗試完全關閉視覺工作室。刪除文件。如果沒有被刪除重啓電腦。 – Matt

+0

打開文件後,如果程序剎車filestream.close()將無法執行,這就是爲什麼它顯示它正在被另一個進程使用。 – Matt