将逗号分隔的字符串逐行写入文件

发布于 2024-11-26 14:02:23 字数 543 浏览 1 评论 0原文

此 cide 旨在从富文本框(即用逗号分隔的用户列表)中获取文本,然后将每个条目写在自己的行上。

然而,事实并非如此。我做错了什么?

if (chkWhiteList.Checked)
        {
            string rawUser = rtboxWhiteList.Text;
            string[] list = rawUser.Split(new char[] { ',' });

            foreach (string user in list)
            {
                using (StreamWriter whiteList = new StreamWriter(cleanDir + @"\white-list.txt"))
                {
                    whiteList.WriteLine(String.Format("{0}\r\n", user));
                }
            }
        }

This cide is meant to take the text from a rich text box (that is a list of users separated by commas) and then write each entry on its own line.

However, it does not. What have I done wrong?

if (chkWhiteList.Checked)
        {
            string rawUser = rtboxWhiteList.Text;
            string[] list = rawUser.Split(new char[] { ',' });

            foreach (string user in list)
            {
                using (StreamWriter whiteList = new StreamWriter(cleanDir + @"\white-list.txt"))
                {
                    whiteList.WriteLine(String.Format("{0}\r\n", user));
                }
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

撩人痒 2024-12-03 14:02:23

我会交换你的 usingfor 循环 。并删除换行符

using (StreamWriter whiteList = new StreamWriter(cleanDir + @"\white-list.txt"))
{
    foreach (string user in list)
    {
        whiteList.WriteLine(user);
    }
}

I would swap your using and for loop around. And remove the new line characters

using (StreamWriter whiteList = new StreamWriter(cleanDir + @"\white-list.txt"))
{
    foreach (string user in list)
    {
        whiteList.WriteLine(user);
    }
}
停滞 2024-12-03 14:02:23

你的 foreach 和你的 using 是错误的。您需要使用来设置流写入器,然后在其中执行循环(foreach)来写入行。

if (chkWhiteList.Checked)
    {
        string rawUser = rtboxWhiteList.Text;
        string[] list = rawUser.Split(new char[] { ',' });

        using (StreamWriter whiteList = new StreamWriter(cleanDir + @"\white-list.txt"))
        {
            foreach (string user in list)
            {
                whiteList.WriteLine(String.Format("{0}\r\n", user));
            }
        }
    }

(这是对你的代码的粗略修改。)

Your foreach and your using are the wrong way round. You need to have the using to set the streamwriter, then do a loop (foreach) wihtin this to write the lines.

if (chkWhiteList.Checked)
    {
        string rawUser = rtboxWhiteList.Text;
        string[] list = rawUser.Split(new char[] { ',' });

        using (StreamWriter whiteList = new StreamWriter(cleanDir + @"\white-list.txt"))
        {
            foreach (string user in list)
            {
                whiteList.WriteLine(String.Format("{0}\r\n", user));
            }
        }
    }

( this is rough hack of your code. )

长不大的小祸害 2024-12-03 14:02:23

试试这个....

    if (chkWhiteList.Checked)
    {
        string rawUser = rtboxWhiteList.Text;
        string[] list = rawUser.Split(new char[] { ',' });

        using (StreamWriter whiteList = new StreamWriter(cleanDir + @"\white-list.txt"))
            {
                foreach (string user in list)
                {
                     whiteList.WriteLine(String.Format("{0}\r\n", user));
                }

            }
    }

try this....

    if (chkWhiteList.Checked)
    {
        string rawUser = rtboxWhiteList.Text;
        string[] list = rawUser.Split(new char[] { ',' });

        using (StreamWriter whiteList = new StreamWriter(cleanDir + @"\white-list.txt"))
            {
                foreach (string user in list)
                {
                     whiteList.WriteLine(String.Format("{0}\r\n", user));
                }

            }
    }
瀞厅☆埖开 2024-12-03 14:02:23
        {
            using (StreamWriter whiteList = new StreamWriter(cleanDir + @"\white-list.txt"))
            {
                whiteList.WriteLine(String.Format("{0}\r\n", user));
            }
        }

这将每次用用户的一行重写该文件。

移动 foreach 周围的 open 语句,将所有用户名写入文件。

        {
            using (StreamWriter whiteList = new StreamWriter(cleanDir + @"\white-list.txt"))
            {
                whiteList.WriteLine(String.Format("{0}\r\n", user));
            }
        }

This will rewrite to the file each time with a single line with the user.

Moving the open statement around the foreach to write all user names out to file.

酒几许 2024-12-03 14:02:23

使用File.WriteAllLines可以非常轻松解决这个问题

string rawUser = rtboxWhiteList.Text;            
string[] list = rawUser.Split(',');
System.IO.File.WriteAllLines(cleanDir + @"\white-list.txt", list);

This can be very easily solved with File.WriteAllLines

string rawUser = rtboxWhiteList.Text;            
string[] list = rawUser.Split(',');
System.IO.File.WriteAllLines(cleanDir + @"\white-list.txt", list);
↙厌世 2024-12-03 14:02:23

WriteLine 调用附加一个 CRLF,但您的 String.Format 包含一个额外的 CRLF。因此,每个用户将获得两行。

并且 using 语句需要位于 foreach 之外(ist 中的字符串 user)。

The WriteLine call appends a CRLF, but your String.Format is including an additional CRLF. So you will get two lines per user.

And the using statement needs to be outside of your foreach (string user in ist).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文