【问题标题】:Environment.SpecialFolder.MyDocuments works but SpecialFolder.MyComputer doesn't work?Environment.SpecialFolder.MyDocuments 有效,但 SpecialFolder.MyComputer 无效?
【发布时间】:2014-10-30 00:45:24
【问题描述】:

我正在使用 VS2013 Express for Desktop 并使用 Windows 窗体。

我关注this MSDN Walkthrough "Creating an Explorer Style Interface with the ListView and TreeView Controls Using the Designer"。所以我试图让树视图从“MyComputer”级别开始,以便用户可以选择所有驱动器号。

适用于 MyDocuments,如下:

DirectoryInfo info = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));

但是当我从自动完成中选择 MyComputer 时它不起作用:

DirectoryInfo info = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyComputer));

我明白了:

在 mscorlib.dll 中出现“System.ArgumentException”类型的未处理异常 附加信息:路径不是合法形式。

这令人惊讶,因为“MyComputer”在自动完成功能中可用,但不像其他特殊文件夹那样工作。

谢谢。

更新: 谢谢 ByteBlast 和 CodyGray。

在这里问我应该怎么做是否合适?

【问题讨论】:

  • 这是学习如何使用调试器的好时机。创建一个额外的变量并将 Environment.GetFolderPath(Environment.SpecialFolder.MyComputer) 的结果放入其中。然后使用调试器检查返回的内容。
  • @gus:我也遇到了同样的问题。您找到任何解决方案或替代方案了吗?

标签: c# windows winforms visual-studio special-folders


【解决方案1】:

The documentation 声明:

MyComputer 常量总是产生空字符串 (""),因为没有 路径是为“我的电脑”文件夹定义的。

DirectoryInfo 构造函数提供一个空字符串是导致ArgumentException 的原因。

Elsewhere on MSDN 你可以找到关于为什么返回一个空字符串的解释:

如果不是操作系统创建的文件夹、现有文件夹被删除或文件夹是虚拟目录(例如我的电脑)与物理路径不对应,则该文件夹实际上不存在。

【讨论】:

  • 面临同样的问题。请问有什么解决方案或替代方案?
猜你喜欢
  • 2017-01-08
  • 2012-05-08
  • 2017-03-15
  • 2011-03-08
  • 2013-11-27
  • 2010-11-29
  • 2017-12-13
  • 2015-01-05
  • 2021-06-29
相关资源
最近更新 更多