要获取5天前 FTP 服务器的文件列表,你可以使用 C# 中的 FtpWebRequestFtpWebResponse 类来连接到 FTP 服务器并获取文件列表。以下是一个示例代码:

using System;
using System.IO;
using System.Net;

class Program
{
    static void Main()
    {
        string ftpUrl = "ftp://example.com";
        string ftpUsername = "username";
        string ftpPassword = "password";
        string folderPath = "/path/to/folder";
        DateTime targetDate = DateTime.Now.AddDays(-5); // 5天前的日期

        try
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUrl + folderPath);
            request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            request.Credentials = new NetworkCredential(ftpUsername, ftpPassword);

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());

            string line = reader.ReadLine();
            while (line != null)
            {
                // 解析文件列表行的信息
                string[] fileInfo = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                string fileName = fileInfo[fileInfo.Length - 1];
                string fileDateString = fileInfo[0] + " " + fileInfo[1] + " " + fileInfo[2];
                DateTime fileDate = DateTime.Parse(fileDateString);

                // 比较文件日期与目标日期
                if (fileDate <= targetDate)
                {
                    Console.WriteLine(fileName);
                }

                line = reader.ReadLine();
            }

            reader.Close();
            response.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: " + e.Message);
        }
    }
}

请确保将 ftpUrl 替换为实际的 FTP 服务器地址,ftpUsernameftpPassword 替换为实际的 FTP 凭据,以及 folderPath 替换为实际的文件夹路径。此代码将打印出 5 天前的所有文件名

c# 获取5天前Ftp服务器的文件列表

原文地址: https://www.cveoy.top/t/topic/hXlv 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录