【问题标题】:Identify whether ftp URL is directory name or file using c#使用c#识别ftp URL是目录名还是文件
【发布时间】:2012-07-19 10:53:46
【问题描述】:

对于 ftp 路径说ftp://ftp.something.com/,我可以使用此代码列出所有目录:

WebRequest req = WebRequest.Create(url) as WebRequest;
        req.Method = WebRequestMethods.Ftp.ListDirectory;

//code to get response from ftp site and list all files and directories path in a list name name_list.

现在 foreach 来自列表 name_list 的路径,如果路径是目录,那么我将该路径添加到列表名称 sub_list 中,否则如果它是某个文件的路径(.txt、.pdf、.rar、.html、.tw 和更多扩展名)然后将该路径添加到另一个列表名称 final_list 中。 到目前为止,我能做的是:

foreach(string url in name_list)
{
 if (Regex.IsMatch(url, ".*?" + @"(\.[A-Za-z]{2,4}$)"))
         //add to sub_list
else
        //add to final_list
}

但这不是实现我的目标的可靠且稳健的方法。 有没有其他最好的方法。

【问题讨论】:

标签: c# ftp


【解决方案1】:

我会使用 System.IO.Path 类来确定它是路径还是文件。具体来说: http://msdn.microsoft.com/en-us/library/system.io.path.getfilename.aspx

【讨论】:

  • 这是FTP相关的,我怀疑你可以使用本地文件系统功能。
  • 提问者正在寻找一种方法来确定字符串 url 是否与目录或文件模式匹配。为此,无论 url 是什么,Path 类都可以工作。
【解决方案2】:

您可以查看文件权限以查看它是否是目录。请参考以下示例:

http://www.copyandwaste.com/posts/view/parsing-webrequestmethodsftplistdirectorydetails-and-listdirectory/

【讨论】:

    猜你喜欢
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    • 2022-09-27
    相关资源
    最近更新 更多