【问题标题】:C#: save treeview content in a fileC#:将树视图内容保存在文件中
【发布时间】:2016-02-07 14:27:40
【问题描述】:

我关注了this topic,但在此行:Stream file = File.Open(filename, FileMode.Create) 出现未处理的异常 (UnauthorizedAccessException)。

编辑:这是我的代码:

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

有人可以帮助我吗?

【问题讨论】:

  • 避免提供外部链接,而是尝试添加sn-p inline。
  • 下次我会记住的,对不起。
  • @Newokmyne 您需要编辑您的问题并提供代码。此外,您需要说明您收到错误的语句是什么。
  • 已编辑。但是现在发现错误

标签: c# treeview


【解决方案1】:

根据MSDN,您在以下情况下收到此异常:

path 指定了一个只读文件,访问权限不是Read。

-或-

path 指定了一个目录。

-或-

调用者没有所需的权限。

-或-

模式为创建,指定文件为隐藏文件。

【讨论】:

  • 考虑到缺乏相关信息,这是最佳答案。投票。
  • 我通过阅读这篇文章发现了我的错误:路径指定了一个目录。因为我的 FileName 属性具有目录值。不是文件名。谢谢。
猜你喜欢
  • 2011-08-17
  • 2018-03-01
  • 1970-01-01
  • 2013-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-10
  • 1970-01-01
相关资源
最近更新 更多