tintincutes
tintincutes

Reputation: 5778

C# notepad program throws "Empty path name is not legal"

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;
        }//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;
        }

Update

Here is the error:

Error: ArgumentException was unhandled. Empty path name is not legal

I get this if I open a text file. Then it highlighted this line code: textBox1.Text=System.IO.File.ReadAllText(filename,System.Text.Encoding.Default).

And if I save nothing happens.

Upvotes: 2

Views: 2381

Answers (5)

bejoy
bejoy

Reputation:

//To declare private int docno //To declare private string filename //To declare private bool modified=false;

          if (modify == true)
            {


                //this.Text = filename;

                filename = saveFileDialog1.FileName;
                sw = new StreamWriter(filename);
                sw.Write(textBox1.Text);


                sw.Close();
                //modify = false;

            }

        else
            {
                saveFileDialog1.FileName = "Untitled" + docno.ToString() + ".txt";
                dresult = saveFileDialog1.ShowDialog();
                docno++;
            }

Upvotes: 0

AviD
AviD

Reputation: 13112

Well, it looks like you're saving with a blank filename - this changes during the call to .ShowDialog(), so it doesnt help that you've retrieved it beforehand.
You just need to pull out .FileName again after .ShowDialog.

Upvotes: 0

Cristian Diaconescu
Cristian Diaconescu

Reputation: 35641

Try moving the line

String filename = save.FileName;

inside the IF block. You are assigning to filename before the SaveDialog's property is set by the user. You need to understand that this line does not create a permanent link between your filename variable and the FileName property of the dialog.

Upvotes: 2

BobbyShaftoe
BobbyShaftoe

Reputation: 28499

You get the filename from a SaveFileDialog after you call ShowDialog. You are setting filename beforehand.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062780

I expect you should be reading the filename after the user has used the dialog:

System.IO.File.WriteAllText(save.FileName, textBox1.Text);

Also - SaveFileDialog is IDisposable, so you should be "using" it...

using (SaveFileDialog save = new SaveFileDialog())
{
    // your code that involves "save"
}

Upvotes: 7

Related Questions