简体   繁体   English

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

[英]Writing a comma separated string line by line to a file

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));
                }
            }
        }

I would swap your using and for loop around. 我会交换您的usingfor loop And remove the new line characters 并删除换行符

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

Your foreach and your using are the wrong way round. 您的foreach和使用方法是错误的。 You need to have the using to set the streamwriter, then do a loop (foreach) wihtin this to write the lines. 您需要使用来设置流编写器,然后进行循环(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));
            }
        }
    }

( this is rough hack of your code. ) (这是您的代码的粗暴修改。)

        {
            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. foreach移动open语句,将所有用户名写出到文件中。

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));
                }

            }
    }

This can be very easily solved with File.WriteAllLines 这可以非常容易地解决File.WriteAllLines

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

The WriteLine call appends a CRLF, but your String.Format is including an additional CRLF. WriteLine调用会附加一个CRLF,但是您的String.Format包含一个附加的CRLF。 So you will get two lines per user. 因此,每个用户将获得两行。

And the using statement needs to be outside of your foreach (string user in ist). 并且using语句需要在您的foreach之外(ist中的字符串用户)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM