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!

Steve
  • 213,761
  • 22
  • 232
  • 286
user1735367
  • 119
  • 1
  • 2
  • 6

2 Answers2

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);
dmck
  • 7,801
  • 7
  • 43
  • 79
  • 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)) – user1735367 Nov 22 '12 at 22:07
  • understand.But still isnt renaming my file with the surname. This is my code for creating the fileStreamWriter outputStream = File.CreateText("filename.Text"); – user1735367 Nov 22 '12 at 22:12
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);
ChrisF
  • 134,786
  • 31
  • 255
  • 325