当前位置:编程学习 > C#/ASP.NET >>

StreamWriter 问题

在一个循环里,
用了StreamWriter sw = new StreamWriter(文件路径,路径名循环一次变化的),
读取之后用了
sw.Flush();
sw.Close();


为什么循环第二次就报错了。
我有关闭sw.close();
--------------------编程问答-------------------- 你的贴下你的循环把..
--------------------编程问答-------------------- --------------------编程问答--------------------  for (int nfile = 1; nfile <= j; nfile++)
            {
                if (!File.Exists(@"d:\data\" + nfile.ToString() + ".txt"))
                {
                    File.Create(@"d:\data\" + nfile.ToString() + ".txt");
                }
                StreamWriter sw = new StreamWriter(@"d:\data\" + nfile.ToString() + ".txt");

                if (nfile == j)
                {
                    //for (int nstart = ((nfile - 1) * 10000) + 1; nstart < lb_Message.Items.Count; nstart++)
                    for (int nstart = ((nfile - 1) * 50) + 1; nstart <= lb_Message.Items.Count; nstart++)
                    {
                        sw.Write(lb_Message.Items[nstart - 1].ToString() + "\r\n");
                    }
                }
                else
                {
                    //for (int nstart = ((nfile - 1)*10000) +1 ; nstart < nfile * 10000; nstart++)
                    for (int nstart = ((nfile - 1) * 50) + 1; nstart <= nfile * 50; nstart++)
                    {
                        sw.Write(lb_Message.Items[nstart - 1].ToString() + "\r\n");
                    }
                }
                sw.Flush();
                sw.Close();
            } --------------------编程问答-------------------- 麻烦帮我看看,已添出循环部分 --------------------编程问答-------------------- 报啥错 --------------------编程问答-------------------- sw.Close()之后,执行一下sw.dispose()
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,