LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

【C#】类来解析WebRequestMethods.Ftp.ListDirectoryDe​​tails

admin
2021年9月1日 9:6 本文热度 3322

WebRequestMethods.Ftp.ListDirectoryDetails方法解析从FtpWebRequest响应返回的响应。如果所有响应都遵循相同的格式,则很容易,但是不同的FTP服务器软件提供不同的响应格式。

例如,一台服务器可能返回:

08-10-11 12:02PM <DIR> Version2

06-25-09 02:41PM 144700153 image34.gif

06-25-09 02:51PM 144700153 updates.txt

11-04-10 02:45PM 144700214 digger.tif

另一台服务器可能会返回:

d--x--x--x 2 ftp ftp 4096 Mar 07 2002 bin

-rw-r--r-- 1 ftp ftp 659450 Jun 15 05:07 TEST.TXT

-rw-r--r-- 1 ftp ftp 101786380 Sep 08 2008 TEST03-05.TXT

drwxrwxr-x 2 ftp ftp 4096 May 06 12:24 dropoff

而且还观察到其他差异,因此可能存在许多我尚未遇到的细微差异。

是否有人知道可以无缝处理这些情况的完全托管(不需要在Windows上访问外部dll)的C#类?

我只需要列出具有以下详细信息的目录内容:文件/目录名称,最近更新或创建的时间戳,文件/目录名称。


该文章在 2021/9/1 9:26:53 编辑过

全部评论1

admin
2021年9月1日 9:20
public static string FTPRegexDir = @"\s*(?<size>[0-9]*)\s*(?<month>(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\s*(?<day>[0-9]*)\s*(?<yearTime>([0-9]|:)*)\s*(?<fileName>.*)";          //FTP解析正则字串
public static string FTPRegexDD = @"^(\d+-\d+-\d+\s+\d+:\d+(?:AM|PM))\s+(<DIR>|\d+)\s+(.+)$";  //FTP解析正则字串

/// <summary>
/// 获取当前目录下的目录及文件
/// </summary>
/// param name="ftpfileList"></param>
/// <param name="dirName"></param>
/// <returns></returns>
public static List<ActFile> GetFtpFile(string dirName, int ilevel = 0)
{
    var ftpfileList = new List<ActFile>();
    return MethodInvoke($@"GetFtpFile({dirName})", () =>
    {
        string tempDir = dirName;
        tempDir = tempDir.Replace("//", "/");
        if (Data.Left(tempDir, 1) == "/") { tempDir = Data.Right(tempDir, tempDir.Length - 1); }
        if (Data.Right(tempDir, 1) == "/") { tempDir = Data.Left(tempDir, tempDir.Length - 1); }
        string url = "ftp://" + ftpHostIP + ":" + ftpHostPort + "/"+ tempDir;
        try
        {
            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri(url));
            ftpRequest.UseBinary = true;
            ftpRequest.Credentials = new NetworkCredential(username, password);
            ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;//该方法可以得到文件名称的详细资源
            StreamReader ftpFileListReader = new StreamReader(ftpRequest.GetResponse().GetResponseStream(), Encoding.UTF8);
            while (!ftpFileListReader.EndOfStream)
            {
                string line = ftpFileListReader.ReadLine();
                string firstChr = Data.Left(line, 1);
                string fileSize = "";
                string fileName = "";
                bool isDirTemp = false;
                if (firstChr == "d" || firstChr == "-")
                {
                    Regex FtpListDirDetailsRegex = new Regex(FTPRegexDir, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    Match match = FtpListDirDetailsRegex.Match(line);
                    fileSize = match.Groups["size"].Value;
                    fileName = match.Groups["fileName"].Value;
                }
                else
                {
                    Regex FtpListDirDetailsRegex = new Regex(FTPRegexDD, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    Match match = FtpListDirDetailsRegex.Match(line);
                    fileSize = match.Groups[2].Value;
                    fileName = match.Groups[3].Value;
                }
                if (firstChr == "d" || line.IndexOf("<DIR>") > -1)
                {
                    fileSize = "0";
                    isDirTemp = true;
                }
                ftpfileList.Add(new ActFile { isDir = isDirTemp, name = fileName, path = "/" + tempDir, level = ilevel, size = fileSize, lineStr = line });
            }
            ftpFileListReader.Close();
        }
        catch (Exception ex)
        {
            ftpfileList.Add(new ActFile { isDir = true, name = "", path = "/" + tempDir, level = ilevel, size = "0", lineStr = "FTP连接错误,错误原因:" + Environment.NewLine + ex.Message });
        }
        return ftpfileList;
    });
}

该评论在 2021/9/1 15:47:14 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved