0

I followed this topic, but I got an unhandled Exception (UnauthorizedAccessException) at this line: Stream file = File.Open(filename, FileMode.Create).

Edit: Here's my code:

public void SaveTree(TreeView tree, string filename)
    {
        using (Stream file = File.Open(filename, FileMode.Create))
        {
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(file, tree.Nodes.Cast<TreeNode>().ToList());
        }
    }

    SaveTree(treeView1, path); //in the SaveButton_Click method

Is someone can help me?

Community
  • 1
  • 1
Newokmyne
  • 65
  • 2
  • 10

1 Answers1

3

According to MSDN you're receiving this exception when:

path specified a file that is read-only and access is not Read.

-or-

path specified a directory.

-or-

The caller does not have the required permission.

-or-

mode is Create and the specified file is a hidden file.

Ion Sapoval
  • 635
  • 5
  • 8
  • This is the best answer considering lack of additional information in question. Voting up. – MaLiN2223 Feb 07 '16 at 14:41
  • I found my mistake by reading this: path specified a directory. Cause my FileName attribute had the directory value. not the file name. Thanks. – Newokmyne Feb 07 '16 at 14:45