开发者

Oddity with System.IO.Directory.GetDirectories

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

S开发者_JAVA百科ystem.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.


My guess is that GetDirectories("D:") gives you the list of directories within the current directory of your D drive, while GetDirectories("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 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.


What is the current directory of drive D:?

It's not the root.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜