繁体   English   中英

C#使StreamWriter写入文本文件

[英]C# Getting StreamWriter to Write to text file

我已经多次复习了我的教授的演讲,复习了教科书,在网络上无休止地寻找答案,并尝试了所有合理的建议修复方法。 我当然想念一些东西。 通讯簿写入列表框,创建文件,但名称和地址未写入文件。 我所缺少的任何帮助将不胜感激。

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

        }
    }
}

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

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

另外,请确保关闭/处置流编写器,以便将更改提交到文件并关闭文件。 通常的做法是using

using (var writer = new StreamWriter("addressbook.txt"))
{
     writer.WriteLine("First textbox:{0}, second:{1}", textBox1.Text, textBox2.Text);
}

尝试在流上调用“刷新”或“关闭”。 最佳做法是将它包装在使用中。

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

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

向下滚动到示例代码。

您忘记添加完整的地址,请尝试以下操作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM