我正在做一个数学测验,我成功地将我的问题和答案保存在不同的文件中。现在我正在尝试将我的问题从我的文件中加载到标签中。我会将文件的每一行加载为不同的问题。
这就是我保存文件的方式:
//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 = "";
} else if (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 frmquestion_Load(object sender, EventArgs e) {
string line;
string[] file = System.IO.File.ReadAllLines(@"C:\Users\User\Desktop\Assignment 2 Solo part\Questions.txt");
line = file.ReadLine();
Console.WriteLine(line);
}
我得到的错误是:
“System.Array”不包含“ReadLine”的定义,并且找不到接受“System.Array”类型的第一个参数的扩展方法“ReadLine”(您是否缺少 using 指令或程序集引用?)