C# 制作一个按钮,为下一次运行程序保存列表

C# 制作一个按钮,为下一次运行程序保存列表,c#,windows,list,save,C#,Windows,List,Save,我正在制作一个windows窗体应用程序。它是一个单词生成器,从默认列表生成一个随机单词,也可以通过用户输入进行修改。我正在寻找一种方法,这样一个按钮将保存列表,以便用户下次运行应用程序时,他们将拥有与以前相同的列表。txtaddverb是用于用户输入的文本框。缺少的按钮只对一系列名词、形容词和副词起相同的作用 下面是我的代码的样子: public class Lists { public static List<string> verbList = ne

我正在制作一个windows窗体应用程序。它是一个单词生成器,从默认列表生成一个随机单词,也可以通过用户输入进行修改。我正在寻找一种方法,这样一个按钮将保存列表,以便用户下次运行应用程序时,他们将拥有与以前相同的列表。txtaddverb是用于用户输入的文本框。缺少的按钮只对一系列名词、形容词和副词起相同的作用

下面是我的代码的样子:

  public class Lists
    {
        public static List<string> verbList = new List<string>() {"eat", "scramble", "slap", "stimulate"};

        public static Random randomverb = new Random();
    }


public string pickRandomVerb()

        {
            return Lists.verbList[Lists.randomverb.Next(0, Lists.verbList.Count)];
        }

public void button1_Click(object sender, EventArgs e)

        {
            if (Lists.verbList.Count > 0) verb.Text = pickRandomVerb();
        }

public void button5_Click(object sender, EventArgs e)

        {
            Lists.verbList.Add(txtaddverb.Text);
            txtaddverb.Clear();
        } 

public void button9_Click(object sender, EventArgs e)

        {
            Lists.verbList.Clear();
            verb.Clear();
            txtaddverb.Clear();
        }

//below is the button that I want to save the list

public static void button13_Click(object sender, EventArgs e)

        {
            //need help here
        }
公共类列表
{
public static List verbList=new List(){“eat”、“scramble”、“slap”、“stimulate”};
public static Random randomverb=new Random();
}
公共字符串选择器谓词()
{
return Lists.verbList[Lists.randomverb.Next(0,Lists.verbList.Count)];
}
公共无效按钮1\u单击(对象发送者,事件参数e)
{
如果(Lists.verbList.Count>0)verb.Text=pickRandomVerb();
}
公共无效按钮5_单击(对象发送者,事件参数e)
{
Lists.verbList.Add(txtaddverb.Text);
txtaddverb.Clear();
} 
公共无效按钮9_单击(对象发送者,事件参数e)
{
Lists.verbList.Clear();
动词Clear();
txtaddverb.Clear();
}
//下面是我想要保存列表的按钮
公共静态无效按钮13_单击(对象发送者,事件参数e)
{
//这里需要帮助吗
}

视情况而定。您想将输入保存到哪里?在文本文件中?在数据库中

保存到文本文件的示例

        // create a writer and open the file
        TextWriter tw = new StreamWriter("date.txt");

        // write a line of text to the file
        tw.WriteLine(DateTime.Now);

        // close the stream
        tw.Close();
要将
列表
写入文件:

File.WriteAllLines(path, verbList);
如果文件不存在,将创建该文件,否则将覆盖该文件

从文件中读取它:

List<string> verbList = File.ReadAllLines(path).ToList();
List verbList=File.ReadAllLines(path.ToList();

如果此项目更适合个人使用,则您可以将列表写入文本文件,并在程序加载时将其读回。可以为此实现streamreader和streamwriter类。有关示例代码,请参见

如果有多人使用你的应用程序,我认为将列表保存到数据库将是一个更好的解决方案。如果您没有SQL server,这里是一个很好的起点。教程可以根据MS Access之类的内容进行调整。(您将使用System.Data.OleDb而不是System.Data.SqlClient)


有很多文章都在读写信息到文本文件或数据库。做一点搜索,如果我发布的两个链接没有你想要的,你应该能够找到适合你需要的东西。

任何最简单的都可以。