0

Ok so I need to write a program that will get contact info from a file and then be able to update that same with a text box. When a new contact is added you should be able to see it as well. With what I have when I create it and then run it using Visual C# 2010 express it comes with a windows error saying the program stopped working. Heres what I have.

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;iterator
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace email
{
    public partial class Form1 : Form
    {
        private System.IO.FileStream outputFileStream;
        private System.IO.StreamWriter streamWriter;
        private System.IO.FileStream inputFileStream;
        private System.IO.StreamReader streamReader;
        public Form1()
        {
            InitializeComponent();
            this.textBox1.Enabled = false;
            this.button1.Enabled = false;
            this.outputFileStream = new System.IO.FileStream(@"contacts.txt", System.IO.FileMode.Append);
            this.streamWriter = new System.IO.StreamWriter(inputFileStream);
            this.inputFileStream = new System.IO.FileStream(@"contacts.txt", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read);
            this.streamReader = new System.IO.StreamReader(inputFileStream);
            this.updateContactList();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            // writes to / updates the contact file
            this.updateContactFile();
            // reads from file / updates the GUI list
            this.updateContactList();
        }
        private void updateContactFile()
        {
            string name = this.textBox1.Text;
            string email = this.textBox2.Text;
            try
            {
                streamWriter.WriteLine(name);
                streamWriter.WriteLine(email);
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.Message);
            }
            this.textBox1.Clear();
            this.textBox2.Clear();
        }
        private void updateContactList()
        {
            this.textBox3.Clear();
            try
            {
                while (streamReader.EndOfStream == false)
                {
                    string name = streamReader.ReadLine();
                    string email = streamReader.ReadLine();
                    string contact = name + Environment.NewLine + email + Environment.NewLine + Environment.NewLine;
                    this.textBox3.AppendText(contact);
                }
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.Message);
            }
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox1.Text.Length > 0)
                this.textBox2.Enabled = true;
            else
                this.textBox2.Enabled = false;
        }
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox2.Text.Length > 0)
                this.button1.Enabled = true;
            else
                this.button1.Enabled = false;
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.streamReader.Close();
            this.streamWriter.Close();
            this.inputFileStream.Close();
            this.outputFileStream.Close();
        }
    }
}
2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by hericles
0

You might want to debug your code to see exactly where the problem occurs but I think it might be because you are opening 2 streams to the same file when the form loads. If you caught the exception you would probably see a message saying the file was already in use by another process.
I believe (but maybe wrong) that if you opened both with FileMode.ReadWrite the problem would fix itself.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.