3

Hi im very new to c# so apologies for this.

Im trying to create a text file to save customers details. But i want to name the text file by the surname. I.e myfile.txt.

Im sure im close, but missing something when it comes to changing the surname.text to a variable then making it the name of my created new file.

I have put 2 ** around the problem area.

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        if ((tb_firstname.Text == "") || (tb_surname.Text == "") || (tb_postcode.Text == ""))
        {
            MessageBox.Show("Missing values from textboxes!");
        }
        else if (
               ****string myfile.txt = (tb_surname.Text);
               File.Exists("myfile.txt"));****
        {
                if (MessageBox.Show("Warning: file already exists. " + 
                                    "Contents will be replaced - " + 
                                     "do you want to continue?", "File Demo",
                                     MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                                     MessageBoxDefaultButton.Button1,
                                     MessageBoxOptions.DefaultDesktopOnly) == 
                    DialogResult.Yes)
                {
                    //write lines of text to file
                    StreamWriter outputStream = File.CreateText(myfile.txt);
                    outputStream.WriteLine(tb_firstname.Text);
                    outputStream.WriteLine(tb_surname.Text);
                    outputStream.WriteLine(tb_surname.Text);
                    outputStream.Close();
                    MessageBox.Show("Text written to file successfully!");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Action cancelled existing file contents not replaced!");
                    this.Close();
                }
            }
            else
            {
                //write lines of text to file
                StreamWriter outputStream = File.CreateText("myFile.txt");
                outputStream.WriteLine(tb_firstname.Text);
                outputStream.WriteLine(tb_surname.Text);
                outputStream.WriteLine(tb_postcode.Text);
                outputStream.Close();
                MessageBox.Show("Text written to file successfully!");
                this.Close();
            }
        }
        catch (Exception problem)
        {
            MessageBox.Show("Program has caused an error - " + problem.ToString());
        }
}

any help would be great!

1
  • You need to get rid of the quotes in your File.Exists() call: File.Exists(myfile.txt) Commented Nov 22, 2012 at 22:35

2 Answers 2

2

You are creating a file called myfile.txt every time

StreamWriter outputStream = File.CreateText("myFile.txt");

That's a string literal you are using.

You have the line:

string myfile.txt = (tb_surname.Text)

which reads the contents of the text box into a variable called myfile.txt. You then need to use that in the file creation code:

StreamWriter outputStream = File.CreateText(myFile.txt);

Note that there are no quotes.

This will overwrite the file if it already exists - if you want to append you will need to use the following method:

StreamWriter outputStream = File.AppendText(myFile.txt);
Sign up to request clarification or add additional context in comments.

Comments

2

I believe you are trying to save the surname as part of the file name. You can generate the filename and check if it exists with the code below:

First generate the filename:

string fileName = string.Format("{0}File.txt", tb_surname.Text);

And then check it using the variable:

   File.Exists(fileName);

2 Comments

try { string fileName = string.format ("{0}File.txt", tb_surname.Text); if (tb_firstname.Text == "" || fileName == "" || tb_postcode.Text == "") { MessageBox.Show("Missing values from textboxes!"); } else if (File.Exists(fileName))
understand.But still isnt renaming my file with the surname. This is my code for creating the fileStreamWriter outputStream = File.CreateText("filename.Text");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.