webby68
webby68

Reputation: 421

C# Getting StreamWriter to Write to text file

I have reviewed my Professor's lecture numerous times, reviewed the text book, searched endlessly online for answers, and tried every reasonable suggested fix out there. I am certainly missing something. The address book writes to the list box, the file is created, but the name and address is not written to the file. Any help with what I am missing would be greatly appreciated.

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

        }
    }
}

Upvotes: 0

Views: 22598

Answers (3)

KF2
KF2

Reputation: 10171

You forgot to add complete address,try this:

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

Upvotes: 1

Alexei Levenkov
Alexei Levenkov

Reputation: 100620

You are saving data to file on form load when all fields are empty. Move writing code to some button click handler after you have chance to type values in the text fields.

You are also using version of TextWriter.WriteLine method that take format as first argument. Unless you really want to do that either 2 separate WriteLine calls or one with correct format string would probably do what you want.

Also make sure to close/dispose your stream writer so it commits changes to file and closes the file. Usual way to do that is to use using:

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

Upvotes: 6

Gjeltema
Gjeltema

Reputation: 4166

Try either calling Flush or Close on the stream. Best practice is to wrap it in a using though.

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

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

Scroll down to the example code.

Upvotes: 1

Related Questions