C#FTP,如何检查Path是文件还是目录?

我有一个包含一些FTP pathes的数组,如下所示:

“FTP // IP /目录/ directory1中”,
“FTP // IP /目录/ directory2”
“FTP // IP /目录/ file.txt的”,
“FTP // IP /目录/ directory3目录”,
“FTP // IP /目录/ another_file.csv”

如何确定路径是文件还是目录?

提前致谢。

使用LIST命令(可以参考RFC959)获取有关指定路径下项目的详细信息。 以FileZilla Server为例,LIST命令将返回标准LINUX权限格式,您可以在此处找到该格式。 第一个字母表示请求的路径是文件还是目录。 此外,还可以在此处找到用C#编写的简单库

一种方法是,如果我们可以假设所有文件都以扩展名结尾,并且所有目录都没有扩展名,我们可以使用System.IO.Path.GetExtension()方法,如下所示:

public bool IsDirectory(string directory) { if(directory == null) { throw new ArgumentNullException(); // or however you want to handle null values } // GetExtension(string) returns string.Empty when no extension found return System.IO.Path.GetExtension(directory) == string.Empty; } 

我有同样的问题。 我解决了休的答案。 你需要做一个FTPRequest,如:

 request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; 

从streamreader抓住它并将其粘在一个字符串中

 StreamReader reader = new StreamReader(responseStream); string directoryRaw = null; try { while (reader.Peek() != -1) { directoryRaw += reader.ReadLine() + "|"; } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } 

当你打印它时,它看起来像:

| -rw-r – r– 1位用户1699 2015年6月1日404.shtml

| drwxr-xr-x 2用户user 4096 Sep 8 19:39 cgi-bin

| drwxr-xr-x 2用户user 4096 11月3日10:52 css

这些是由|分开的 这将是splitstring的delim

如果它以ad开头而不是a – 那么它是一个目录,否则就是一个文件。

这些在文件名之前都是相同的大小,因此为从位置62到结尾的每个字符串创建一个新字符串,这将是文件名。

希望能帮助到你

没有直接的方法。

间接地,您可以假设没有句点“。”的文件名。 是目录,但这不会永远是真的。

最好是编写仔细使用这些路径的代码,以便将路径视为目录,然后如果FTP服务器报告错误,则将其视为文件。

您可以使用System.IO.Path.GetExtension(path) `来检查您的路径是否具有文件扩展名。

给定“ftp // ip / directory / directory1”或“ftp // ip / directory / directory2 /”,GetExtension将返回一个String.Empty给你。

这不是万无一失的,但是如果有一个没有扩展名的文件可能会完全崩溃,或者带有句点的目录可能会导致问题。

我发现“hack”如何确定目标类型。 如果你愿意的话

 request.Method = WebRequestMethods.Ftp.GetFileSize; 

在文件夹上,它将导致exception

未处理的exception:System.Net.WebException:远程服务器返回错误r:(550)文件不可用(例如,找不到文件,没有访问权限)。

但是在文件中使用它,它自然会返回它的大小。

我已经为这种方法创建了示例代码。

  static bool IsFile(string ftpPath) { var request = (FtpWebRequest)WebRequest.Create(ftpPath); request.Method = WebRequestMethods.Ftp.GetFileSize; request.Credentials = new NetworkCredential("foo", "bar"); try { using (var response = (FtpWebResponse)request.GetResponse()) using (var responseStream = response.GetResponseStream()) { return true; } } catch(WebException ex) { return false; } } 

您可能想要更改它,因为这将捕获任何FTP错误。

我遇到了同样的问题所以我使用GetFileSize来检查它是文件还是目录

var isFile = FtpHelper.IsFile(“ftpURL”,“userName”,“password”);

 using System; using System.Net; public static class FtpHelper { public static bool IsFile(Uri requestUri, NetworkCredential networkCredential) { return GetFtpFileSize(requestUri, networkCredential) != default(long); //It's a Directory if it has no size } 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. } public static long GetFtpFileSize(Uri requestUri, NetworkCredential networkCredential) { //Create ftpWebRequest object with given options to get the File Size. var ftpWebRequest = GetFtpWebRequest(requestUri, networkCredential, WebRequestMethods.Ftp.GetFileSize); try { return ((FtpWebResponse)ftpWebRequest.GetResponse()).ContentLength; } //Incase of success it'll return the File Size. catch (Exception) { return default(long); } //Incase of fail it'll return default value to check it later. } }