如何用C#写入文件? [英] How to write to file in C#?

查看:81
本文介绍了如何用C#写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我遇到问题,我使用Visual Studio2010.我总是在Listview中显示结果,但现在我想将其保存在文件中.你能帮助我吗?如何将数据保存到文件?

谢谢

Dear all,

I got a problem, I use Visual studio 2010. I always show my result in Listview, but now I want to save it in a file. Can you help me? How to save a data to a file?

Thanks

推荐答案

听起来您希望将结果存储在List< t>中.以及您的ListView,然后将列表的内容写到文件中.即
It sounds like you will want to store your results in a List<t> as well as your ListView and then write the contents of the list out to a file. i.e.
List<string> resultData = new List<string>();
resultData.Add("Some result data");


等...


etc...

using (StreamWriter writer = new StreamWriter("pathOfFileToSaveDataTo"))
{
    for (int i = 0; i < resultData.Count; i++)
    {
        writer.WriteLine(resultData[i]);
    }

    writer.Close();
}



希望对您有所帮助.


您也可以只将ListView的内容写出到文件中,而不必将结果存储在List< T>中.如果需要的话.



hope this helps.


You could also just write the contents of the ListView out to the file without storing the results in a List<T> if you wanted to.


这篇关于如何用C#写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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