【发布时间】:2016-03-07 10:15:49
【问题描述】:
我发现一个代码非常有用,但是下面的代码从一个 FTP 服务器返回目录和文件的名称,我只需要获取文件的名称。
ftpRequest = (FtpWebRequest) FtpWebRequest.Create(host + "/" + directory);
/* Log in to the FTP Server with the User Name and Password Provided */
ftpRequest.Credentials = new NetworkCredential(user, pass);
/* When in doubt, use these options */
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
/* Specify the Type of FTP Request */
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
/* Establish Return Communication with the FTP Server */
ftpResponse = (FtpWebResponse) ftpRequest.GetResponse();
/* Establish Return Communication with the FTP Server */
ftpStream = ftpResponse.GetResponseStream();
/* Get the FTP Server's Response Stream */
StreamReader ftpReader = new StreamReader(ftpStream);
/* Store the Raw Response */
string directoryRaw = null;
/* Read Each Line of the Response and Append a Pipe to Each Line for Easy Parsing */
try{
while (ftpReader.Peek() != -1){
directoryRaw += ftpReader.ReadLine() + "|";
}
}
catch(Exception ex)
{
//Do something
}
...
...
...
我调查,但WebRequestMethods.Ftp 只有ListDirectory 和ListDirectoryDetails,都返回目录和文件的名称:(..
有人可以帮助我..
谢谢
【问题讨论】:
-
你不能用
Path.GetFileName吗? -
在哪里?,在 FTP 中?我认为 GetFileName 用于本地文件
-
试试
Path.GetFileName("ftp://127.0.0.1/folder/filename.txt"):o) -
使用 sshnet 检查这个线程:stackoverflow.com/questions/11781808/…
标签: c# ftp ftpwebrequest