如何在C#中使用FTP列出目录内容?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在C#中使用FTP列出目录内容?相关的知识,希望对你有一定的参考价值。
如何在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","janeDoe@contoso.com");
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();
}
}
答案
试试这个:
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(uri);
ftpRequest.Credentials =new NetworkCredential("anonymous","janeDoe@contoso.com");
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();
它给了我一个目录列表...所有列在目录字符串列表中......告诉我这是否是你需要的
另一答案
你可能正在寻找PrintWorkingDirectory
另一答案
您需要列出目录内容的ListDirectory
编辑:或者你可以使用这个Chilkat库,很好地为你包装
另一答案
一些代理重新格式化目录列表,因此除非可以保证代理不会更改,否则很难可靠地解析目录列表
另一答案
在以下链接中有一个方法GetDirectoryInformation(),它从FTP目录递归地获取文件和目录。
Getting files from FTP directory recursively
另一答案
获取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.
}
另一答案
如果要列出de目录中文件的名称,则必须在调用之前放置(reqFTP.Proxy = null;)(reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;)。
希望这可以帮到你!
以上是关于如何在C#中使用FTP列出目录内容?的主要内容,如果未能解决你的问题,请参考以下文章