首页 新闻 会员 周边

StreamReader的问题

0
悬赏园豆:10 [已关闭问题]

        //保存表项到txt文件----------------------------
        private void menuItem5_Click(object sender, EventArgs e)
        {
            //string file = this.saveFileDialog1.FileName;
            string file = "aa.txt";
            FileInfo fi = new FileInfo(file);
            StreamWriter sw = fi.CreateText();
            sw.WriteLine("名称\t单价");
           
            for (int i = 0; i < this.listView1.Items.Count; i++)
            {
                sw.WriteLine("{0}\t{1}", listView1.Items[i].Text, listView1.Items[i].SubItems[1].Text);
            }
            sw.Close();
            MessageBox.Show("数据保存成功!", "恭喜你:");
        }

初次接触c#以上是我写的把istView控件的数据保存到txt文件,如何按照原格式读入到istView?谢谢!

近在眼前的主页 近在眼前 | 初学一级 | 园豆:189
提问于:2010-05-10 13:00
< >
分享
其他回答(1)
0

我想你是不知道如何使用 StreamReader 吧。这里是我的代码。

代码
using (StreamReader reader = new StreamReader("[Your file path]"))
{
string line = null;
while ((line = reader.ReadLine()) != null)
{
// Your code to analyze the line and add to list view
}
}

 

更多内容可以参考下面的 MSDN。

http://msdn.microsoft.com/en-us/library/system.io.streamreader(v=VS.100).aspx

http://msdn.microsoft.com/en-us/library/k3352a4t(v=VS.100).aspx

周巍 | 园豆:735 (小虾三级) | 2010-05-10 14:04
我会循环读入一行,是当时没找到分割文本的方法,现在解决了,谢谢参与的朋友。 FileStream filest = new FileStream(@"aa.txt", FileMode.Open, FileAccess.ReadWrite); StreamReader sr = new StreamReader(filest); string strLine = sr.ReadLine();//读取文件中的一行 while (strLine != null)//判断是否为空,表示到文件最后一行了 { //处理代码 strLine = sr.ReadLine(); } sr.Close();//关闭流 filest.Close();
支持(0) 反对(0) 近在眼前 | 园豆:189 (初学一级) | 2010-05-10 16:56
0

可以一行行“reader.ReadLine()”读,也可以一下子全部读出来到string,格式是存在的。

Astar | 园豆:40805 (高人七级) | 2010-05-10 14:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册