Hi all i am using a save dialog box to save a text file to a loction becuse i cant hard code in the driectory. My problem is that i can save the first line of text that i write to the file but i want then to be able to save save another line of text but it ask me to over write the file is there any way to get around this so that i can save as many lines as i want .

My code:

private void btnSave_Click(object sender, EventArgs e)
        {
            Stream myStream;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;


            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = saveFileDialog1.OpenFile()) != null)
                {
                    firstName = txtFirstName.Text;
                    lastName = txtLastName.Text;
                    phoneNoArea = Convert.ToInt32(txtAreaCode.Text);
                    phoneNo = Convert.ToInt32(txtPhoneNumber.Text);
                    birthMonth = txtBirthMonth.Text;
                    birthDay = Convert.ToInt32(txtDateOfBirth.Text);
                    Writer.WriteLine(firstName + Record + lastName + Record + phoneNoArea + Record + phoneNo + Record + birthMonth + Record + birthDay + Record);

                    // Clear the Text Boxes
                    txtFirstName.Clear();
                    txtLastName.Clear();
                    txtAreaCode.Clear();
                    txtPhoneNumber.Clear();
                    txtBirthMonth.Clear();
                    txtDateOfBirth.Clear();
                    MessageBox.Show("File Saved");
                    myStream.Close();
                }
            }

        }

Line 21: What is Writer?

you have to use streamWriter in this case

example usage:(stream writer)

using System;
using System.IO;

class Test 
{
	
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";
        try 
        {
            if (File.Exists(path)) 
            {
                File.Delete(path);
            }

            using (StreamWriter sw = new StreamWriter(path)) 
            {
                sw.WriteLine("This");
                sw.WriteLine("is some text");
                sw.WriteLine("to test");
                sw.WriteLine("Reading");
            }

            using (StreamReader sr = new StreamReader(path)) 
            {
                while (sr.Peek() >= 0) 
                {
                    Console.WriteLine(sr.ReadLine());
                }
            }
        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}

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

I cant hard code in the address of the file

writer is to write lines of text to the file i am going to make and save with the save dialog box but when i try to save another line of text the the same file it asks me to over write the file but i don't want to over write the file i want to add text to the file just. The reason i am using save dialog box it that i can't hard code in the directory

A really simple solution.

private void AppendToFile(string path, string contents)
{
    try
    {
        StreamWriter sw = new StreamWriter(new FileStream(path, FileMode.Append, FileAccess.Write));
        sw.Write(contents);
        sw.Close();
    }
    catch (Exception e)
    { 
        Console.WriteLine(e.Message); 
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.