Directory.GetFiles 有效,Directory.GetDirectories 不在同一目录上

发布于 2024-07-17 18:19:30 字数 763 浏览 6 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清晨说晚安 2024-07-24 18:19:31

如果没有权限,可以强制抛出异常:

new FileIOPermission(FileIOPermissionAccess.PathDiscovery | FileIOPermissionAccess.Read,dir).Demand();

You can force an exception to be thrown if you do not have permission:

new FileIOPermission(FileIOPermissionAccess.PathDiscovery | FileIOPermissionAccess.Read,dir).Demand();
旧伤还要旧人安 2024-07-24 18:19:30

目录是隐藏的吗?

它们是实际目录,还是(因为这是网络服务器)虚拟文件夹?

顺便说一句 - 除非您特别想要某些 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 original string[] arrays... indeed, with 3.5, LINQ blurs the advantages of List<T>, since Where(...), First(...) etc apply to arrays.

嘴硬脾气大 2024-07-24 18:19:30

您是否检查过该目录是否位于您认为的位置? 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文