从文件c#中删除空行

时间:2013-05-24 16:01:41

标签: c#

好的,我尝试了几个代码。我只是无法弄清楚这里要做什么.. 我正在做一个测验。我保存这样的问题和答案。

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

        }

直到这里一切都很好。问题是在我删除一个问题后,文件中有空行,我尝试了很多东西。我改变了2次我保存的方式。我仍然可以使它工作。由于创建了空白空间,因此删除问题时,我也出现了一个越界错误。 这是删除代码:

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


         }
     }

那么如何删除空行?

1 个答案:

答案 0 :(得分:1)

删除行后再保存到文件而不是:

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