0

I've made a button so when I click it, it opens up a directory from which I can create a text file, I click the button, it opens the directory fine, I can name the text file what I want and click save, but then I get an error saying "An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll"

This is my code:

    private void createAlgorithmsAndComplexityNotesToolStripMenuItem_Click(object sender, EventArgs e)
    {
        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "Text File|*.txt";
        sfd.FileName = "Algorithms And Complexity Lecture Notes";
        sfd.Title = "Algorithms And Complexity Lecture Notes";
        if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string path = sfd.FileName;
            StreamWriter write = new StreamWriter(File.Create("C:\\Users\antho\\Desktop\\Folder\\Uni\\Programming and data structures\\Assignment 2\\Modules"));
            write.Write(writeFile);
            write.Close();
        }
    }
6
  • using the debugger.. what line does it throw the error..? is it this line StreamWriter write = new StreamWriter(File.Create("C:\\Users\antho\\Desktop\\Folder\\Uni\\Programming and data structures\\Assignment 2\\Modules")); Commented Mar 23, 2016 at 18:52
  • @MethodMan yes it's that line, it also says there are Illegal characters in path. Commented Mar 23, 2016 at 18:53
  • that's because you have spaces in your file path probably why don't you try writing to a file path like "@c:\Users\antho\" + path when do you ever use the path..meaning creating the FileName look at this post for an even easier way to create the filename stackoverflow.com/questions/27823789/… Commented Mar 23, 2016 at 18:55
  • also you need to have a trailing "\\" in your super long file path.. once again you never even create a file because you never tell the File.Create what the FilePath + FileName.txt is.. Commented Mar 23, 2016 at 18:58
  • 1
    first of all, you dont use the filename, the savedialog gives you, but you allways write to a file named Modules. Maybe there is already an folder existing with that name. Second, you forgot to escape the second backslash in your filenam (the one before antho). I'm not sure whether or not \a is a valid character. But it's definitly not a valid character in a file path, Commented Mar 23, 2016 at 19:03

2 Answers 2

0

First of all put your path in a variable and check if the directory exists at all (if not, create it).

(note the single backslashes)

string path = @"C:\Users\antho\Desktop\Folder\Uni\Programming and data structures\Assignment 2\Modules";
string fileName = sfd.FileName;

if (!Directory.Exists(path))
{
    Directory.CreateDirectory(path);
}

Secondly, use Path.Combine() for constructing paths. The path that you set in the File.Create must end with a filename.

I also recommend using File.WriteAllText() for simple file creation (like in your code). With that you don't have to bother with closing it.

File.WriteAllText(Path.Combine(path, fileName), writeFile);
Sign up to request clarification or add additional context in comments.

Comments

0

Your problem is with the file path you provided.

"C:\\Users\antho\\Desktop\\Folder\\Uni\\Programming and data structures\\Assignment 2\\Modules"

You forgot to escape a backslash here: Users\antho.

In this case, \a is interpreted as a single character, which is illegal in a file name. Not even sure if that is an existing character.

Comments

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.