将列表保存到txt文件 [英] Saving lists to txt file

查看:351
本文介绍了将列表保存到txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将列表保存到文本文件.

I'm trying to save a list to a text file.

这是我的代码:

public void button13_Click(object sender, EventArgs e)
{
    TextWriter tw = new StreamWriter("SavedLists.txt");

    tw.WriteLine(Lists.verbList);
    tw.Close();
}

这是我在文本文件中看到的:

This is what I get in the text file:

System.Collections.Generic.List`1 [System.String]

System.Collections.Generic.List`1[System.String]

我必须使用ConvertAll<>吗?如果是这样,我不确定如何使用.

Do I have to use ConvertAll<>? If so, I'm not sure how to use that.

推荐答案

假设您的通用列表的类型为String:

Assuming your Generic List is of type String:

TextWriter tw = new StreamWriter("SavedList.txt");

foreach (String s in Lists.verbList)
   tw.WriteLine(s);

tw.Close();

或者,使用using关键字:

Alternatively, with the using keyword:

using(TextWriter tw = new StreamWriter("SavedList.txt"))
{
   foreach (String s in Lists.verbList)
      tw.WriteLine(s);
}

这篇关于将列表保存到txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆