Newokmyne
Newokmyne

Reputation: 65

C#: save treeview content in a file

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?

Upvotes: 0

Views: 435

Answers (1)

Ion Sapoval
Ion Sapoval

Reputation: 635

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.

Upvotes: 3

Related Questions