0
我发现一个代码非常有用,但下面的代码从一个FTP服务器返回目录和文件的名称,我只需要获取文件的名称。FtpWebRequest只返回带有ListDirectoryDetails的文件名
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'](https://msdn.microsoft.com/library/system.io.path.getfilename%28v=vs.110%29.aspx)? – cubrr
哪里?,在FTP?我认为GetFileName是为本地文件 – VhsPiceros
尝试'Path.GetFileName(“ftp://127.0.0.1/folder/filename.txt”)':o) – cubrr