0

我已经多次复习教授的讲座,复习课本,在网上无休止地搜索答案,并尝试了所有合理的建议解决方法。我当然错过了一些东西。地址簿写入列表框,文件被创建,但名称和地址没有写入文件。对我所缺少的任何帮助将不胜感激。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;


namespace LAB7A
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }


        List<Person> people = new List<Person>();
        class Person
        {
            public string Name
            {
                get;
                set;
            }

            public string Email
            {
                get;
                set;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            try
            {
               StreamWriter address = new StreamWriter("addressbook.txt");
               address.WriteLine(textBox1.Text, textBox2.Text);
            }
            catch (DirectoryNotFoundException exc)
            {
                MessageBox.Show("Directory Could Not Be Found");
            }
        }

        private void button1_Click(object sender, System.EventArgs e)

        {

            Person p = new Person();
            p.Name = textBox1.Text;
            p.Email = textBox2.Text;
            people.Add(p);
            listBox1.Items.Add(p.Name);
            listBox1.Items.Add(p.Email);



            //StreamWriter address.WriteLine(textBox1.Text);
            //address.WriteLine(textBox2.Text);
            //listBox1.Items.Add(textBox1.Text);
            //listBox1.Items.Add(textBox2.Text);
        }

        private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
        {

        }
    }
}
4

3 回答 3

6

当所有字段为空时,您在表单加载时将数据保存到文件。在您有机会在文本字段中键入值后,将编写代码移至某个按钮单击处理程序。

您还使用了将格式作为第一个参数的TextWriter.WriteLine方法的版本。除非您真的想这样做,否则 2 个单独的WriteLine调用或一个具有正确格式字符串的调用可能会满足您的需求。

还要确保关闭/处置您的流编写器,以便它提交对文件的更改并关闭文件。通常的方法是使用using

using (var writer = new StreamWriter("addressbook.txt"))
{
     writer.WriteLine("First textbox:{0}, second:{1}", textBox1.Text, textBox2.Text);
}
于 2013-04-21T04:00:17.127 回答
1

尝试在流上调用 Flush 或 Close。最佳做法是将其包装在 using 中。

using(StreamWriter address = new StreamWriter("addressbook.txt"))
{
    ...
}

http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx

向下滚动到示例代码。

于 2013-04-21T02:10:55.327 回答
1

你忘了添加完整的地址,试试这个:

 private void Window_Loaded(object sender, RoutedEventArgs e)
    {

        try
        {

           using (StreamWriter address = new StreamWriter("d:\\addressbook.txt"))//Add correct address
            {
              address.WriteLine(textBox1.Text + textBox2.Text);
              //OR
              address.WriteLine(string.Format("Name:{0}, Email:{1}", textBox1.Text,    textBox2.Text));
            }
        }
        catch (DirectoryNotFoundException exc)
        {
            MessageBox.Show("Directory Could Not Be Found");
        } 
    } 
于 2013-04-21T03:05:38.720 回答