Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#从文本框中删除行,并删除空行_C#_String_File_Windows Forms Designer - Fatal编程技术网

c#从文本框中删除行,并删除空行

c#从文本框中删除行,并删除空行,c#,string,file,windows-forms-designer,C#,String,File,Windows Forms Designer,我有一个保存文件内容的文本框。我有一个按钮,当按下它时,它会根据字符串删除一行。代码如下: private void deleteModuleButton_Click(object sender, EventArgs e) { String newText = String.Empty; List<String> lines = fileContentTextBox.Text.Split(new String[] { Environme

我有一个保存文件内容的文本框。我有一个按钮,当按下它时,它会根据字符串删除一行。代码如下:

    private void deleteModuleButton_Click(object sender, EventArgs e)
    {
        String newText = String.Empty;
        List<String> lines = fileContentTextBox.Text.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
        lines.RemoveAll(str => str.Contains(deleteModuleComboBox.Text));
        lines.ForEach(str => newText += str + Environment.NewLine);
        fileContentTextBox.Text = newText;
    }
private void deleteModuleButton_单击(对象发送方,事件参数e)
{
String newText=String.Empty;
List lines=fileContentTextBox.Text.Split(新字符串[]{Environment.NewLine},StringSplitOptions.RemoveEmptyEntries.ToList();
RemoveAll(str=>str.Contains(deleteModuleComboBox.Text));
ForEach(str=>newText+=str+Environment.NewLine);
fileContentTextBox.Text=newText;
}
但是,当删除一行时,它会在文本框底部留下一个空行。问题是,当文件保存时。由于多余的空行,无法再次读取。当我试图打开一个在删除一行后保存的文件时,会出现这个错误:“附加信息:索引超出了数组的边界”。读取文件时,我将每一行作为字符串,然后使用“(',”)拆分该行

有没有办法更改代码,使其接受“newText”并添加新行,但不接受最后一个字符串?或者其他方式,只是为了在结尾没有空行

谢谢

编辑:我让它工作了(谢谢L.B)

更新和工作代码:

    private void deleteModuleButton_Click(object sender, EventArgs e)
    {
        String newText = String.Empty;
        List<String> lines = fileContentTextBox.Text.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
        lines.RemoveAll(str => str.Contains(deleteModuleComboBox.Text));
        newText = string.Join(Environment.NewLine, lines);
        fileContentTextBox.Text = newText;
    }
private void deleteModuleButton_单击(对象发送方,事件参数e)
{
String newText=String.Empty;
List lines=fileContentTextBox.Text.Split(新字符串[]{Environment.NewLine},StringSplitOptions.RemoveEmptyEntries.ToList();
RemoveAll(str=>str.Contains(deleteModuleComboBox.Text));
newText=string.Join(Environment.NewLine,行);
fileContentTextBox.Text=newText;
}
更换

lines.ForEach(str => newText += str + Environment.NewLine);

替换

lines.ForEach(str => newText += str + Environment.NewLine);


您是否尝试过
fileContentTextBox.Text=string.Join(Environment.NewLine,lines)
?您是否尝试过
fileContentTextBox.Text=string.Join(Environment.NewLine,lines)
?非常感谢。当我添加
newText=string.Join(Environment.NewLine,lines)时,我也为自己工作就在
行.ForEach之后(str=>newText+=str+Environment.NewLine)
但我将替换它,谢谢:)@Dr4ken它之所以有效是因为
string.Join
code通过为newtext指定一个新值来覆盖以前的代码非常感谢。当我添加
newText=string.Join(Environment.NewLine,lines)时,我也为自己工作就在
行.ForEach之后(str=>newText+=str+Environment.NewLine)
但我会替换它,谢谢:)@Dr4ken它之所以有效,是因为
string.Join
code通过为newText指定一个新值来覆盖以前的代码