DirectoryInfo.GetDirectories()缺少某些目录 [英] DirectoryInfo.GetDirectories() misses some Directories

查看:252
本文介绍了DirectoryInfo.GetDirectories()缺少某些目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下命令获取目录的所有子目录:
DirectoryInfo.GetDirectories("*",SearchOption.AllDirectories).

在我测试过的所有文件夹上,这将返回正确的结果
但是

如果您在"C:\ Windows \ System32"上执行此操作,则会发现2个问题:

1.(已解决)关于您没有权限的文件夹,不记得该名称,但是如果您通过浏览器浏览路径,Windows会要求获得许可,一旦获得,一切正常. br/>
2.再次运行它,您将看到它无例外地执行
但是
目录丢失!!!
没有异常抛出..没什么

搜索了一段时间后,我发现了一个缺少的目录.
例如,以"C:\ Windows \ System32 \ appmgmt"为例,我想它在所有计算机上都存在.

奇怪的是,如果您使用sourceDirectory ="C:\ Windows \ System32 \ appmgmt"执行[DirectoryInfo.GetDirectories(..)]

您将最终得到DirectoryNotFoundException和优美的消息:
找不到路径"C:\ Windows \ System32 \ appmgmt \"的一部分."

目录在那里...它只是看不到它或不喜欢它或... :(

快速下载解决方案文件以供自己查看: http://www.filesonic.com/file/1034796551/DSS_ -_Copy.rar [^ ]

解决方案

我看不到GetDirectories的任何问题.再说一次,我是我自己的计算机上的管理员,没有运行UAC.

据我所知,没有管理员身份并且无法以管理员身份运行代码,就无法获得所有文件夹.如果您在计算机上启用了UAC,则代码将以普通用户帐户运行,直到UAC启动并提示您进行提升.也许您应该将清单添加到应用中,然后尝试这种方式.阅读 [ ^ ]找出方法.


我找到了解决方法.

不是一些安全的文件夹..
如果是这样,即使Windows资源管理器也不会显示em.

无论如何,带有或不带有Sp1的Visual Studio 2010默认情况下都具有平台目标" x86.
因此,如果您在x64机器上运行此程序,则Framework会执行类似兼容模式的操作,从而导致错误的结果.
即使您尝试添加文件夹浏览器"对话框控制,您最终仍会再次看到某些文件夹丢失.

因此,如果仅将平台目标"设置为任何CPU",问题就不存在了. />
http://alphafs.codeplex.com/ [ ^ ]


I''m trying to get all the subDirectories of a directory using the:
DirectoryInfo.GetDirectories("*",SearchOption.AllDirectories).

On all folders i have tested this is returns correct results
BUT

If u execute this on "C:\Windows\System32 you will find 2 problems:

1.(Solved) Is about a folder that u don''t have permisions, don''t remember the name but if you browse the path through the explorer, Windows asks to get permission and once got, It all works Fine.

2.Running it again you will see that it executes with no exceptions
BUT
Directories are missing !!!
No exception thrown..Nothing

After searching it for a while i found a directory that''s missing.
"C:\Windows\System32\appmgmt" for example, I suppose it exists on all machines.

The strange thing is that if you execute the [DirectoryInfo.GetDirectories(..)] with the sourceDirectory = "C:\Windows\System32\appmgmt"

you will end up with a DirectoryNotFoundException and the elegant message:
"Could not find a part of the path ''C:\Windows\System32\appmgmt\''."

The directory is there...It just doesn''t see it or like it or...:(

Quick Download Solution File to see for yourself : http://www.filesonic.com/file/1034796551/DSS_-_Copy.rar[^]

解决方案

I can''t see any problem with GetDirectories. Then again, I''m an admin on my own machine and don''t have UAC running.

From what I can tell, you cannot get all the folders without being an admin on the machine and running the code as an admin. If you have UAC enabled on your machine, your code is running as a normal user account until UAC kicks in and prompts you for elevation. Maybe you should add a manifest to the app and try it that way. Read this[^] to find out how.


I found the solution.

It''s not some folders that are secured or anything..
If it was this even Windows Explorer wouldn''t show em.

Anyway Visual Studio 2010 with or without Sp1 has by default "Platform Target" x86.

So if you run this on a x64 machine, the Framework does something like compatibility mode causing the faulty Results.
Even if you try adding a Folder Browser dialogue contol you will eventually see that again some Folders are missing.

So if you just set the "Platform Target" to "Any CPU", the problem is gone..Duh


Post is old but for future reference here is the solution i found:

http://alphafs.codeplex.com/[^]


这篇关于DirectoryInfo.GetDirectories()缺少某些目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆