c# - Environment.SpecialFolder.MyDocuments 有效但 SpecialFolder.MyComputer 无效?

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

我正在使用 VS2013 Express for Desktop 和 Windows Forms。

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

它适用于 MyDocuments,如下所示:

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

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

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

我得到:

An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll Additional information: The path is not of a legal form.

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

谢谢。

更新: 谢谢 ByteBlast 和 CodyGray。

在这里问我应该做什么合适吗?

最佳答案

The documentation指出:

The MyComputer constant always yields the empty string ("") because no path is defined for the My Computer folder.

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

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

A folder will not physically exist if the operating system did not create it, the existing folder was deleted, or the folder is a virtual directory, such as My Computer, which does not correspond to a physical path.

关于c# - Environment.SpecialFolder.MyDocuments 有效但 SpecialFolder.MyComputer 无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25679513/

相关文章:

c# - 如何打印用户选择的文档?

c# - 人数统计系统

c# - 根据 N 个值中的最小值返回不同的结果

C#:动态添加字段到类

windows - 如何查询 netstat 以查找 Windows 7 中特定本地地址的 PID?

windows - 按下 R.Alt+键时如何防止激活 Ctrl+Alt+键绑定(bind)?

c# - 解决方案不在 Visual Studio 中运行/构建/重建

mysql - 无法连接到 Windows 上 X.X.X.X 上的 mysql 服务器

winforms - Visual Studio Intellisense 一直在骗我

c# - MenuItem 的颜色变化