C#:如何将组合框等项目列表保存到 .NET 设置文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/376622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:10:12  来源:igfitidea点击:

C#: How do you save a list of items like a Combobox to the .NET Settings file?

c#

提问by

C#: How do you save a list of items like a Combobox to the .NET Settings file?

C#:如何将组合框等项目列表保存到 .NET 设置文件?

回答by Zachary Yates

If you are talking about the Application User Settings, I'd loop through the combobox and save the values in a delimited string:

如果您在谈论应用程序用户设置,我会遍历组合框并将值保存在分隔字符串中:

StringBuilder sb = new StringBuilder();
foreach(var item in combo.Items){
  sb.Append(item.ToString() + ";");
}
Properties.Settings.MyListSetting = sb.ToString();

Please excuse if the above code isn't perfect, It's just an example.

如果上面的代码不完美,请原谅,这只是一个例子。

Hope that helps!

希望有帮助!

回答by foson

The only collection type of use that the Settings Designer lets you use is System.Collections.ArrayList. If you do use an ArrayList, all of its elements' types must be serializable (have the [Serializable] attribute or implement System.Runtime.Serialization.ISerializable.)

设置设计器允许您使用的唯一集合类型是 System.Collections.ArrayList。如果您确实使用 ArrayList,则其所有元素的类型都必须是可序列化的(具有 [Serializable] 属性或实现 System.Runtime.Serialization.ISerializable。)

Here's some code to get data from an ArrayList (named cboCollection) in Settings into a combo box and back.

下面是一些代码,用于从设置中的 ArrayList(名为 cboCollection)获取数据到组合框并返回。

    private void Form1_Load(object sender, EventArgs e)
    {
        if (Settings.Default.cboCollection != null)
            this.comboBox1.Items.AddRange(Settings.Default.cboCollection.ToArray());
    }


    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        ArrayList arraylist = new ArrayList(this.comboBox1.Items);
        Settings.Default.cboCollection = arraylist;
        Settings.Default.Save();
    }

    //A button to add items to the ComboBox
    private int i;
    private void button1_Click(object sender, EventArgs e)
    {
        this.comboBox1.Items.Add(i++);
    }

回答by Guido

The Windows Forms objects are not serializable. Therefore you cannot serialize and store them in a file with a binaryformatter. You need to store the combobox values manually in a file.

Windows 窗体对象不可序列化。因此,您无法使用 binaryformatter 将它们序列化并存储在文件中。您需要将组合框值手动存储在文件中。

string comboboxFileName = @"c:\workDir\settings.settings";

private void saveComboboxInFile (String comboboxFileName )
{
   //--------------------------------------------------------
   //- Store the combobox values in a file. 1 value = 1 line
   //--------------------------------------------------------
   try
    {
        using (StreamWriter comboboxsw = new StreamWriter(comboboxFileName))
        {
            foreach (var cfgitem in comboBox.Items)
            {
                comboboxsw.WriteLine(cfgitem);
            }
        } // End Using`
    }
    catch (Exception e)
    {
       //process exception
    }
}



private void reloadCombboxFromFile (string  comboboxFileName )
   {
    //-------------------------------------------------
    //- Read the values back into the combobox
    //------------------------------------------------- 
        try
        {
            using (StreamReader comboboxsr = new StreamReader(comboboxFileName))
            {
                 while (!comboboxsr.EndOfStream)
                 {
                      string itemread = comboboxsr.ReadLine();
                      comboBox.Items.Add(itemread);
                 }
            } // End Using
      }
      catch (DirectoryNotFoundException dnf)
      {
         // Exception Processing
      }
      catch (FileNotFoundException fnf)
      {
         // Exception Processing
      }
      catch (Exception e)
      {
         // Exception Processing
      }
   }