Directory.GetFiles 有效,Directory.GetDirectories 不在同一目录上
我的 Web 应用程序上有一个简单的文件选择器,它使用 Directory.GetFiles 和 Directory.GetDirectories 来生成 UI。 这在我的本地主机上完美运行,但是当我将其上传到我的 Windows Server 2003 托管时,我只能看到文件 - 在同一目录中,GetFiles 可以工作,但 GetDirectories 不能。
代码非常简单 - 字符串 dir 是由 Server.MapPath 调用创建的,然后:
List<string> dirs = Directory.GetDirectories(dir).ToList();
List<string> files = Directory.GetFiles(dir).ToList();
我已经尝试了所有我能想到的安全方面的方法; 甚至到了给予“每个人”对网络根目录中所有目录的完全访问权限的程度,即使这也没有什么区别。
因此,如果问题不在于安全性,我将非常感谢您提供一些建议,以尝试更多的事情!
更新:我很愚蠢 - 生成 HTML 的代码包含一些非常旧的测试代码,这些代码在本地没有任何区别,因此没有被注意到和删除,但这导致了每个服务器上的目录被忽略! 它以前如何?
if (!subDir.Contains(".")) { ...
在服务器上,所有站点都位于以其域命名的文件夹中 - 在本地,它们不是。 我==傻瓜。 对不起大家!
I have a simple file selector on my web application which uses Directory.GetFiles and Directory.GetDirectories to produce the UI. This works perfectly on my localhost, but when I upload it to my Windows Server 2003 hosting I can only see files - on the same directory, GetFiles works but GetDirectories doesn't.
The code is incredibly simple - the string dir is created by a Server.MapPath call, and then:
List<string> dirs = Directory.GetDirectories(dir).ToList();
List<string> files = Directory.GetFiles(dir).ToList();
I have tried everything I can think of security-wise; even to the point of giving "Everyone" full access to all directories in the web root, and even this makes no difference.
So if the issue is not with security, I would be very grateful of some suggestions for more things to try!
Update: I'm pretty dumb - the code that spat out the HTML contained some very old testing code that hadn't made any difference on local so hadn't been noticed and removed, but that caused every directory on the server to be ignored! What was it?
if (!subDir.Contains(".")) { ...
On the server, all sites are in folders named by their domain - on local, they aren't. Me == stupid. Sorry everyone!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果没有权限,可以强制抛出异常:
You can force an exception to be thrown if you do not have permission:
目录是隐藏的吗?
它们是实际目录,还是(因为这是网络服务器)虚拟文件夹?
顺便说一句 - 除非您特别想要某些
List
功能,否则您不妨将它们保留在原始string[]
数组中......确实,对于 3.5, LINQ 模糊了List
的优点,因为Where(...)
、First(...)
等适用于数组。Are the directories hidden?
Are they actual directories, or (since this is a web-server) virtual folders?
Btw - unless you specifically want some of the
List<T>
features, you might as well leave them in the originalstring[]
arrays... indeed, with 3.5, LINQ blurs the advantages ofList<T>
, sinceWhere(...)
,First(...)
etc apply to arrays.您是否检查过该目录是否位于您认为的位置? IIS 通常会将您置于临时目录中,因此它可能已将您的所有文件复制到此临时文件夹,但没有复制任何子目录,因此根本就没有子目录。
Have you checked that the directory is where you think it is? IIS can often put you in a temporary directory, so it may have copied all your files over to this temp folder, but none of the subdirs, so there simply are none.