使用Streamwriter将数据行写入.txt文件 [英] Writing datarows to a .txt file using streamwriter

查看:293
本文介绍了使用Streamwriter将数据行写入.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来很简单的逻辑问题,但是很难弄清楚.

我正在使用StreamWriter写入.txt文件,并且工作正常.事情是,我要遍历各行,并且需要在循环后写一个"9",但是在设置方式上,每行都会打印一个"9".

写入所有行后,如何将"9"写入文件?这是代码.谢谢!

I have a logic issue that seems so easy, but am having trouble trying figure it out.

I am writing to a .txt file using a StreamWriter and that is working fine as it should. The thing is, is I am looping through the rows and need to write a "9" after the looping, but the way it is set up, a "9" gets printed for every row.

How can I write the "9" to the file after all the rows are written? Here is the code. Thank you!

foreach (DataRow dr in dt.Rows)
                        {
                            if (dr[12].ToString().Contains("55|"))
                            {
                                string file55 = "C:\\authorize.netTEST\\55-Authorize.Net - " + date1 + " @ " + date2 + ".ach";
                                StreamWriter sw = new StreamWriter(file55, true);//inetpub\temp
                                if (dr[0].ToString() != "2")
                                {
                                    if (dr[9].ToString() != "Total Amount")
                                    {
                                        sw.Write("6                            ");
                                        sw.Write(dr[9].ToString().Replace(".", "").Replace(",", "").PadLeft(10, '0'));
                                        sw.Write((dr[12].ToString().Replace("55|","")).PadRight(15, ' '));
                                        sw.Write((dr[13].ToString() + " " + dr[14].ToString()).PadRight(40));
                                        sw.Write(sw.NewLine);
                                    }
                                }

                                    sw.Write("9");
                                    sw.Write(sw.NewLine);

                                sw.Close();

                                Session.Add("DatedFileName", file55);
                            }if (dr[12].ToString().Contains("99|"))
                                {
                                string file99 = "C:\\authorize.netTEST\\99-Authorize.Net - " + date1 + " @ " + date2 + ".ach";
                                StreamWriter sw = new StreamWriter(file99, true);//inetpub\temp
                                if (dr[0].ToString() != "2")
                                {
                                    if (dr[9].ToString() != "Total Amount")
                                    {
                                        sw.Write("6                            ");
                                        sw.Write(dr[9].ToString().Replace(".", "").Replace(",", "").PadLeft(10, '0'));
                                        sw.Write((dr[12].ToString().Replace("99|", "")).PadRight(15, ' '));
                                        sw.Write((dr[13].ToString() + " " + dr[14].ToString()).PadRight(40));
                                        sw.Write(sw.NewLine);
                                    }
                                }
                                    sw.Write("9");
                                    sw.Write(sw.NewLine);
                                 sw.Close();
                                Session.Add("DatedFileName2", file99);
                            }
                         }
                }

推荐答案

我知道了.我只使用了两个foreach循环,每个需要创建的文件一个.我以为我以前曾尝试过,但一定不能正确地做过.

我仍将不胜感激.
I figured it out. I just used two foreach loops, one for each file needing to be created. I thought I had tried that before but must not have done it properly.

I would still appreciate any advice or suggestions.


这篇关于使用Streamwriter将数据行写入.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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