C# 从网络目录获取文件名和目录列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15964983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:37:19  来源:igfitidea点击:

Get file names and directory list from web directory

c#asp.net

提问by Thirusanguraja Venkatesan

I want to get all files names and directory list from one web directory through http://using C#.

我想通过http://使用 C#从一个 web 目录中获取所有文件名和目录列表。

For example:

例如:

Directory.GetFiles(folder)**

is used to get files list from local directory, same as i want to get files list from web directory.

用于从本地目录获取文件列表,与我想从 Web 目录获取文件列表相同。

回答by Sajeetharan

You can't use Directory.GetFileson a URL.

您不能Directory.GetFiles在 URL 上使用。

Consider the example:

考虑这个例子:

string baseURL = "http://download.example.org/export/dump/";
WebClient client = new WebClient();            
string content = client.DownloadString(baseURL);

Then you run a loop inside.

然后你在里面运行一个循环。

回答by Soner G?nül

Well, I don't believe HTTP has a "directory index"you can directly use.

好吧,我不相信 HTTP 有一个"directory index"可以直接使用的。

You can't pass urlto Directory.GetFilesmethod, this method takes as a parameter physical path of url(if it is accessible of course).

您不能传递urlDirectory.GetFiles方法,该方法将url 的物理路径作为参数(当然如果可以访问)。

Check out Server.MapPathmethod instead.

Server.MapPath改为查看方法。

The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server.

Path

Specifies the relative or virtual path to map to a physical directory. If Path starts with either a forward (/) or backward slash (), the MapPath method returns a path as if Path were a full, virtual path. If Path doesn't start with a slash, the MapPath method returns a path relative to the directory of the .asp file being processed.

MapPath 方法将指定的相对或虚拟路径映射到服务器上相应的物理目录。

小路

指定映射到物理目录的相对或虚拟路径。如果 Path 以正斜杠 (/) 或反斜杠 () 开头,则 MapPath 方法返回一个路径,就好像 Path 是完整的虚拟路径一样。如果 Path 不以斜杠开头,则 MapPath 方法返回相对于正在处理的 .asp 文件的目录的路径。

回答by Aron

HTTP in itself does NOT have any function to list the FILE content in a folder. This is impossible, for one thing, as the HTTP specification does not require a URL to be mapped TO a folder. The whole basis of programmable webpages is based on a URL NOT being mapped to a STATIC file.

HTTP 本身没有任何功能来列出文件夹中的 FILE 内容。这是不可能的,一方面,因为 HTTP 规范不要求将 URL 映射到文件夹。可编程网页的整个基础基于未映射到静态文件的 URL。

However. There ARE many extensions to the HTTP protocol. WebDAV IS a protocol for mapping an HTTP URI to a directory in some location. Check out here for how to query a WebDAV service for the content of a folder.

然而。HTTP 协议有很多扩展。WebDAV 是一种将 HTTP URI 映射到某个位置的目录的协议。查看此处了解如何查询 WebDAV 服务以获取文件夹的内容

However it can't do recursive queries as far as I know.

但是,据我所知,它不能进行递归查询。

回答by massi

System.IO.Directory.GetFiles(HttpContext.Current.Server.MapPath("~/PortalPage/"))