System.IO.Directory.GetDirectories 的奇怪之处
这就是我今天在进行目录搜索时注意到的一件事,这可能会让一些人感到困惑。
我发现在我的 Windows XP 计算机上
System.IO.Directory.GetDirectories("C:\") 给了我 17 个文件夹
System.IO.Directory.GetDirectories("C:") 给了我 17 个文件夹
System.IO.Directory.GetDirectories ("D:\") 给了我 12 个文件夹
System.IO.Directory.GetDirectories("D:") 给了我 0 个文件夹
我试图找出为什么我的搜索缺少我的 D 驾驶。
Just something I noticed today when doing directory searches that might trip some people up.
I found on my Windows XP machine that
System.IO.Directory.GetDirectories("C:\") gave me 17 folders
System.IO.Directory.GetDirectories("C:") gave me 17 folders
System.IO.Directory.GetDirectories("D:\") gave me 12 folders
System.IO.Directory.GetDirectories("D:") gave me 0 folders
I was trying to figure out why my search was missing my D drive.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的猜测是,
GetDirectories("D:")
为您提供 D 驱动器当前目录内的目录列表,而GetDirectories("D:\ ")
为您提供 D 驱动器根 中的目录列表。如果您的 D 盘根目录有 12 个目录,并且当前目录位于您的 D 盘上并且其中没有目录,那么这些就是您应该期望的结果。
My guess is that
GetDirectories("D:")
gives you the list of directories within the current directory of your D drive, whileGetDirectories("D:\")
gives you the list of directories in the root of your D drive.If your D drive's root has 12 directories and the current directory is on your D drive and has no directories in it, those are the results you should expect.
Windows 历史上提供了与 DOS 的向后兼容性,DOS 将仅包含驱动器号而不包含目录的路径视为对驱动器“当前目录”的引用。但由于每个驱动器实际上没有当前目录(根据 Raymond Chen) 我的猜测是它没有返回任何内容,因为该进程的当前目录位于 C: 驱动器上。尝试将Environment.CurrentDirectory 设置为D:\,然后看看是否得到相同的结果。
Windows has historically provided backwards compatibility with DOS which treated paths that contained only a drive letter without a directory as a reference to the drive's "current directory". But since there isn't actually a current directory per drive (cmd.exe simulates this, according to Raymond Chen) my guess is that it's returning nothing because the current directory for the process is on the C: drive. Try setting Environment.CurrentDirectory to D:\ then see if you get the same results.
D盘当前目录是什么?
这不是根。
What is the current directory of drive D:?
It's not the root.