如何在C#中使用FTP列出目录内容?

问题描述 投票:40回答:7

如何在C#中使用FTP列出目录内容?

我使用下面的代码用FTP列出目录内容它以XML格式返回结果,但我只想要目录的名称而不是整个内容。

我怎么能这样做?

public class WebRequestGetExample
{
    public static void Main ()
    {
        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/");
        request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

        // This example assumes the FTP site uses anonymous logon.
        request.Credentials = new NetworkCredential ("anonymous","[email protected]");

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        Console.WriteLine(reader.ReadToEnd());

        Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription);

        reader.Close();
        response.Close();
    }
}

MSDN

c# .net ftp directory-listing
7个回答
57
投票

试试这个:

FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(uri);
ftpRequest.Credentials =new NetworkCredential("anonymous","[email protected]");
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());

List<string> directories = new List<string>();

string line = streamReader.ReadLine();
while (!string.IsNullOrEmpty(line))
{
    directories.Add(line);
    line = streamReader.ReadLine();
}

streamReader.Close();

它给了我一个目录列表...所有列在目录字符串列表中......告诉我这是否是你需要的


1
投票

你可能正在寻找PrintWorkingDirectory


1
投票

您需要列出目录内容的ListDirectory

编辑:或者你可以使用这个Chilkat库,很好地为你包装


1
投票

一些代理重新格式化目录列表,因此除非可以保证代理不会更改,否则很难可靠地解析目录列表


0
投票

在以下链接中有一个方法GetDirectoryInformation(),它从FTP目录递归地获取文件和目录。

Getting files from FTP directory recursively


0
投票

获取FTP目录内容的最简单,最有效的方法:

var contents = GetFtpDirectoryContents(new Uri(“ftpDirectoryUri”),new NetworkCredential(“userName”,“password”));

    public static List<string> GetFtpDirectoryContents(Uri requestUri, NetworkCredential networkCredential)
    {
        var directoryContents = new List<string>(); //Create empty list to fill it later.
        //Create ftpWebRequest object with given options to get the Directory Contents. 
        var ftpWebRequest = GetFtpWebRequest(requestUri, networkCredential, WebRequestMethods.Ftp.ListDirectory);
        try
        {
            using (var ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse()) //Excute the ftpWebRequest and Get It's Response.
            using (var streamReader = new StreamReader(ftpWebResponse.GetResponseStream())) //Get list of the Directory Contentss as Stream.
            {
                var line = string.Empty; //Initial default value for line.
                do
                {
                    line = streamReader.ReadLine(); //Read current line of Stream.
                    directoryContents.Add(line); //Add current line to Directory Contentss List.
                } while (!string.IsNullOrEmpty(line)); //Keep reading while the line has value.
            }
        }
        catch (Exception) { } //Do nothing incase of Exception occurred.
        return directoryContents; //Return all list of Directory Contentss: Files/Sub Directories.
    }

    public static FtpWebRequest GetFtpWebRequest(Uri requestUri, NetworkCredential networkCredential, string method = null)
    {
        var ftpWebRequest = (FtpWebRequest)WebRequest.Create(requestUri); //Create FtpWebRequest with given Request Uri.
        ftpWebRequest.Credentials = networkCredential; //Set the Credentials of current FtpWebRequest.

        if (!string.IsNullOrEmpty(method))
            ftpWebRequest.Method = method; //Set the Method of FtpWebRequest incase it has a value.
        return ftpWebRequest; //Return the configured FtpWebRequest.
    }

-1
投票

如果要列出de目录中文件的名称,则必须在调用之前放置(reqFTP.Proxy = null;)(reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;)。

希望这可以帮到你!

© www.soinside.com 2019 - 2024. All rights reserved.