C#WinForms:类型' System.IO.IOException'的未处理异常发生

时间:2017-04-06 18:07:10

标签: c# winforms

我们有一个项目来执行添加和删除大学的计划,并在.txt文件中包含五所大学,以便在ComboBox中显示这些大学。
但它不起作用并显示此错误:

  

未处理的类型' System.IO.IOException'发生在mscorlib.dll

注意:它是一个Windows窗体 所以这里是代码:

public partial class Form1 : Form
{
    university[] univ = new university[10];
    struct university
    {
      public string uni;
      public string prov;
      public string city;
      public string population;
      public string programs;
      public string tuition;
      public string residence;
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void pictureBox4_Click(object sender, EventArgs e)
    {
        MessageBox.Show("If you want to check the infomation, click on the combo box and then choose the University of your choice.\n- Click the black button to add a university to the list after filling in everything. \n- Click the red button to remove a university after selecting it.");
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        StreamReader sr = new StreamReader("Universities.txt"); // object
        String line;
        try 
        {
            for (int i = 0; i < univ.Length; i++)
            {
                line = sr.ReadLine();                   
                string[] sPlit = line.Split(',');
                univ[i].uni = sPlit[0];
                univ[i].prov = sPlit[1];
                univ[i].city = sPlit[2];
                univ[i].population = sPlit[3];
                univ[i].programs = sPlit[4];
                univ[i].tuition = sPlit[5];
                univ[i].residence = sPlit[6];
                comboBox1.Items.Add(univ[i].uni);                  
            }
            sr.Close();                   
        }
        catch (Exception p)   //catches errors
        {
            Console.WriteLine("Exception: " + p.Message);
        }
        finally               //final statement before closing
        {
            Console.WriteLine("Executing finally block.");
        }
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBox1.SelectedIndex == 0)
        {
            listBox1.Items.Clear();
            pictureBox1.Image = Properties.Resources._1;
            listBox1.Items.Add("Province: " + univ[0].prov);
            listBox1.Items.Add("City: " + univ[0].city);
            listBox1.Items.Add("Population: " + univ[0].population);
            listBox1.Items.Add("Programs: " + univ[0].programs);
            listBox1.Items.Add("Tuition: $" + univ[0].tuition);
            listBox1.Items.Add("Residence: $" + univ[0].residence);
        }
        else if (comboBox1.SelectedIndex == 1)
        {
            listBox1.Items.Clear();
            pictureBox1.Image = Properties.Resources._2;
            listBox1.Items.Add("Province: " + univ[1].prov);
            listBox1.Items.Add("City: " + univ[1].city);
            listBox1.Items.Add("Population: " + univ[1].population);
            listBox1.Items.Add("Programs: " + univ[1].programs);
            listBox1.Items.Add("Tuition: $" + univ[1].tuition);
            listBox1.Items.Add("Residence: $" + univ[1].residence);
        }
        else if (comboBox1.SelectedIndex == 2)
        {
            listBox1.Items.Clear();
            pictureBox1.Image = Properties.Resources._3;
            listBox1.Items.Add("Province: " + univ[2].prov);
            listBox1.Items.Add("City: " + univ[2].city);
            listBox1.Items.Add("Population: " + univ[2].population);
            listBox1.Items.Add("Programs: " + univ[2].programs);
            listBox1.Items.Add("Tuition: $" + univ[2].tuition);
            listBox1.Items.Add("Residence: $" + univ[2].residence);
        }
        else if (comboBox1.SelectedIndex == 3)
        {
            listBox1.Items.Clear();
            pictureBox1.Image = Properties.Resources._4;
            listBox1.Items.Add("Province: " + univ[3].prov);
            listBox1.Items.Add("City: " + univ[3].city);
            listBox1.Items.Add("Population: " + univ[3].population);
            listBox1.Items.Add("Programs: " + univ[3].programs);
            listBox1.Items.Add("Tuition: $" + univ[3].tuition);
            listBox1.Items.Add("Residence: $" + univ[3].residence);
        }
        else if (comboBox1.SelectedIndex == 4)
        {
            listBox1.Items.Clear();
            pictureBox1.Image = Properties.Resources._5;
            listBox1.Items.Add("Province: " + univ[4].prov);
            listBox1.Items.Add("City: " + univ[4].city);
            listBox1.Items.Add("Population: " + univ[4].population);
            listBox1.Items.Add("Programs: " + univ[4].programs);
            listBox1.Items.Add("Tuition: $" + univ[4].tuition);
            listBox1.Items.Add("Residence: $" + univ[4].residence);
        }
        else
        {
            pictureBox1.Image = Properties.Resources.noimage;
            listBox1.Items.Clear();
        } 
    }

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {   
    }

    private void label9_Click(object sender, EventArgs e)
    {
    }

    private void label8_Click(object sender, EventArgs e)
    {
    }

    private void label7_Click(object sender, EventArgs e)
    {
    }

    private void label6_Click(object sender, EventArgs e)
    {
    }

    private void label5_Click(object sender, EventArgs e)
    {
    }

    private void label4_Click(object sender, EventArgs e)
    {
    }

    private void label3_Click(object sender, EventArgs e)
    {
    }

    private void textBox8_TextChanged(object sender, EventArgs e)
    {
    }

    private void textBox7_TextChanged(object sender, EventArgs e)
    {
    }

    private void textBox6_TextChanged(object sender, EventArgs e)
    {
    }

    private void textBox5_TextChanged(object sender, EventArgs e)
    {
    }

    private void textBox4_TextChanged(object sender, EventArgs e)
    {
    }

    private void textBox3_TextChanged(object sender, EventArgs e)
    {
    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
    }

    private void AddButton_Click(object sender, EventArgs e)
    {
        int u = 4;
        int p = 4;
        int c = 4;
        int pop = 4;
        int pro = 4;
        int t = 4;
        int r = 4;
        univ[u+1].uni =  textBox1.Text;
        univ[p + 1].prov = textBox2.Text ;
        univ[c + 1].city =  textBox3.Text ;
        univ[pop + 1].population = textBox4.Text ;
        univ[pro + 1].programs =  textBox5.Text;
        univ[t + 1].tuition = textBox6.Text;
        univ[r + 1].residence = textBox7.Text ;
        StreamWriter sw = new StreamWriter("Universities.txt", true);
        String line;
        line = Console.ReadLine();

        sw.WriteLine(univ[u + 1].uni + ", " + univ[p + 1].prov + ", " + univ[c + 1].city + ", " + univ[pop + 1].population + "," + univ[pro + 1].programs
            + ", " + univ[t + 1].tuition + "," + univ[r + 1].residence + ",");
        MessageBox.Show("A University has been added.");
        sw.Close();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("There are " + comboBox1.Items.Count + " universities.");

    }
}
}

1 个答案:

答案 0 :(得分:0)

    try 
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader("TestFile.txt")) 
                {
                    string line;
                    // Read and display lines from the file until the end of 
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null) 
                    {
                        //your code goes here
                string[] sPlit = line.Split(',');
                univ[i].uni = sPlit[0];
                univ[i].prov = sPlit[1];
                univ[i].city = sPlit[2];
                univ[i].population = sPlit[3];
                univ[i].programs = sPlit[4];
                univ[i].tuition = sPlit[5];
                univ[i].residence = sPlit[6];
                comboBox1.Items.Add(univ[i].uni);
                    }
                }
            }
            catch (Exception p) //catches errors
        {
            Console.WriteLine("Exception: " + p.Message);
        }
        finally //final statement before closing
        {
            Console.WriteLine("Executing finally block.");
        }

Hope Helps