从c#文件中删除空行
本文关键字:删除 文件 | 更新日期: 2023-09-27 18:15:27
好的,我尝试了几个代码。我真不知道该怎么办。我在做测验。我就这样保存问题和答案。
private void btnsave_Click(object sender, EventArgs e)
{
//checking if question or answer textbox are empty. If they are not then the question is saved
if (txtquestion.Text != "" & txtanswer.Text != "")
{
//saves the question in the questions text
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:'Users'User'Desktop'Assignment 2 Solo part'Questions.txt", true))
{
file.WriteLine(txtquestion.Text);
}
//saves the answer in the answers text
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:'Users'User'Desktop'Assignment 2 Solo part'Answers.txt", true))
{
file.WriteLine(txtanswer.Text);
}
MessageBox.Show("Question and Answer has been succesfully added in the Quiz!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.None);
//cleaning the textboxes for a new question and answer
txtanswer.Text = "";
txtquestion.Text = "";
}
//checks if the question textbox is empty and shows the corresponding message
else if (txtquestion.Text == "")
MessageBox.Show("Please enter a question", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else //checks if the answer textbox is empty and shows the corresponding message
if (txtanswer.Text == "")
MessageBox.Show("Please enter an answer", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
在此之前一切都很好。问题是在我删除一个问题后,文件中有空行,我尝试了很多东西。我改变了两次保存它们的方式。我仍然可以让它工作。现在我也得到一个越界错误,当我删除一个问题,因为所创建的空白空间。下面是删除代码:
private void fmrdelete_Load(object sender, EventArgs e)
{//declaration
int i=0;
//loading values to array from file
string[] qlines = System.IO.File.ReadAllLines(@"C:'Users'User'Desktop'Assignment 2 Solo part'Questions.txt").ToArray();
foreach (string line in qlines)
{
Console.WriteLine(line);
//saving each line to array possition
questions[i] = line;
i++;
lstboxquestions.Items.Add(line);
}
i = 0;
//loading values to array from file
string[] alines = System.IO.File.ReadAllLines(@"C:'Users'User'Desktop'Assignment 2 Solo part'Answers.txt").ToArray();
foreach (string line in alines)
{
Console.WriteLine(line);
//saving each line to array possition
answers[i] = line;
i++;
}
}
private void btndelete_Click(object sender, EventArgs e)
{
//declarations
int line_to_delete;
int count;
int i=0;
//changing the value given from textbox to integer and then saving it
line_to_delete = Convert.ToInt32(txtans.Text);
//checking to find the question we want to delete
for (count = 0; count < 20; count++)
{
if (count == (line_to_delete-1))
{
questions[count]= "";
}
}
//clearing file
System.IO.File.WriteAllText(@"C:'Users'User'Desktop'Assignment 2 Solo part'Questions.txt",string.Empty);
//saving to file
for (count = 0; count < 20; count++)
{
//saves the question in the questions text
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:'Users'User'Desktop'Assignment 2 Solo part'Questions.txt", true))
{
file.WriteLine(questions[count]);
}
}
//clearing the listbox
lstboxquestions.Items.Add(" ");
//loading from file back to array
string[] qlines = System.IO.File.ReadAllLines(@"C:'Users'User'Desktop'Assignment 2 Solo part'Questions.txt").ToArray();
foreach (string line in qlines)
{
line.Replace("'n'n", "'n");
Console.WriteLine(line);
//saving each line to array possition
questions[i] = line;
i++;
lstboxquestions.Items.Add(line);
}
//checking to find the answers we want to delete
for (count = 0; count < 20; count++)
{
if (count == (line_to_delete - 1))
{
answers[count] = "";
}
}
//clearing file
System.IO.File.WriteAllText(@"C:'Users'User'Desktop'Assignment 2 Solo part'Answers.txt", string.Empty);
//saving to file
for (count = 0; count < 20; count++)
{
//saves the question in the questions text
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:'Users'User'Desktop'Assignment 2 Solo part'Answers.txt", true))
{
file.WriteLine(answers[count]);
}
}
string[] alines = System.IO.File.ReadAllLines(@"C:'Users'User'Desktop'Assignment 2 Solo part'Answers.txt").ToArray();
foreach (string line in alines)
{
Console.WriteLine(line);
//saving each line to array possition
answers[i] = line;
i++;
}
}
}
那么如何删除空行呢
当您删除行然后保存到文件而不是:
file.WriteLine(questions[count]);
试题:
if(!string.IsNullOrWhiteSpac(questions[count]))
file.WriteLine(questions[count]);
对答案做同样的处理。
编辑:你有没有试过:
file.WriteLine(answers[count]);
:
if(!string.IsNullOrWhiteSpac(answers[count]))
file.WriteLine(answers[count]);