Hi

I'm new to programming and I'm starting to create a simple notepad, with only 4 buttons (Open, Save, New and Font).

If I open or save I'm getting an error:
This is my code:

//Declare save as a new SaveFileDailog
            SaveFileDialog save = new SaveFileDialog();
            //Declare filename as a String equal to the SaveFileDialog's FileName
            String filename = save.FileName;
            //Declare filter as a String equal to our wanted SaveFileDialog Filter
            String filter = "Text Files|*.txt|All Files|*.*";
            //Set the SaveFileDialog's Filter to filter
            save.Filter = filter;
            //Set the title of the SaveFileDialog to Save
            save.Title = "Save";
            //Show the SaveFileDialog
            if (save.ShowDialog(this) == DialogResult.OK)
            {
                //Write all of the text in txtBox to the specified file
                System.IO.File.WriteAllText(filename, textBox1.Text);
            }
            else
            {
                //Return
                return;
            }

Any idea?

Thanks for your help in advance

Regards

It would be greatly appreciated, that in the case you have an error you would tell us WHAT error you are experiencing!
The only thing I can come up with(after a long look at your code) is that filename is empty when you call System.IO.File.WriteAllText, perhaps use save.FileName here?

oopsss sorry about that i'm supposed to attached the jpg file for the error.
But I don't know what happened. it just didn't attach my file
The error I'm getting was:
"ArgumentException was unhandled" and the code line:
"System.IO.File.WriteAllText(filename, textBox1.Text);" was highlighted

thanks

Did you read the rest of the error message?
Most of the time you get invaluable information about what went wrong.
I tested your code with filename empty I got :
"ArgumentException was unhandled" but also: Empty path name is not legal.
Debug your code to see what happens! Set a breakpoint before the call to WriteAllText.

This article has been dead for over six months. Start a new discussion instead.