c# - 获取文件列表

标签 c# ftp

我正在尝试获取 FTP 文件夹上的文件列表。 当我在本地运行代码时,代码可以正常工作,但是在部署它时,我开始接收 html 而不是文件名

ArrayList fName = new ArrayList();
try
{
    StringBuilder result = new StringBuilder();

    //create the directory
    FtpWebRequest requestDir =
        (FtpWebRequest) FtpWebRequest.Create(new Uri(directory));
    requestDir.Method = WebRequestMethods.Ftp.ListDirectory;
    requestDir.Credentials = new NetworkCredential(FTP_USER_NAME, FTP_PASSWORD);
    requestDir.UsePassive = true;
    requestDir.UseBinary = true;
    requestDir.KeepAlive = false;
    requestDir.Proxy = null;
    FtpWebResponse response = (FtpWebResponse)requestDir.GetResponse();
    Stream ftpStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(ftpStream, Encoding.ASCII);

    while (!reader.EndOfStream)
    {
        fName.Add(reader.ReadLine().ToString());
    }

    response.Close();
    ftpStream.Close();
    reader.Close();

最佳答案

您可以尝试使用GetFileName

Uri uri = new Uri(hreflink);
string filename = Path.GetFileName(uri.LocalPath);

关于c# - 获取文件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12836297/

相关文章:

C# - 将接口(interface)转换为备用基类?

java - 来自单个连接中多个文件的 Commons FTPClient InputStream

c# - 找到最低的子文件夹并压缩它们

c# - 错误 : Index was outside the bounds of the array in a data set C# and T-SQL

php - file_get_contents 和 file_put_contents 的 FTP 超时

azure - FTP文件上传错误: An exception occurred during a WebClient request. InnerException : This method is not supported.(参数 'value')

java - Java中通过FTP协议(protocol)复制远程文件(使用sendCommand cp -p到Linux)

c - 在 C 中通过套接字发送大文件

c# - 查看将在 HttpResponseMessage/HttpRequestMessage (System.Net.Http, WebAPI) 中发送/接收的原始 header

c# - 更改单个 ASP.NET Core Controller 的 JSON 序列化设置