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();
        }
    }
Anthony
  • 61
  • 1
  • 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"));` – MethodMan Mar 23 '16 at 18:52
  • @MethodMan yes it's that line, it also says there are Illegal characters in path. – Anthony Mar 23 '16 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 http://stackoverflow.com/questions/27823789/creating-text-file-in-c-sharp – MethodMan Mar 23 '16 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.. – MethodMan Mar 23 '16 at 18:58
  • So you have invalid characters in path, then check your path. For instance, shouldn't **\antho** be **\\anto**. – Ivan Stoev Mar 23 '16 at 19:03
  • 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, – derpirscher Mar 23 '16 at 19:03

2 Answers2

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);
Mitulát báti
  • 2,086
  • 5
  • 23
  • 37
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.

Mihai Caracostea
  • 8,336
  • 4
  • 27
  • 46