gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:47:45 24 4
gpt4 key购买 nike

我正在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com