【问题标题】:How to get last write file name based on the file name?如何根据文件名获取最后写入文件名?
【发布时间】:2013-06-12 03:53:15
【问题描述】:

首先,我有一个 ftp 服务器中的文件名列表:

20130612_00
20130612_06
20130612_12

在 Main() 中,我有这样的东西

Public Main()
{
    //function to download all file from ftp server to local directory
    DownLoadFileFromFTP();

    //function to open the latest file in the local directory
    OpenLatestFileInLocal();
}

我想要一个根据本地目录中的文件名检查最新文件的功能。在这种情况下,最新的文件将是20130612_12

我的想法是首先删除特殊字符并只获取数字编号,我现在有一个像这样的list<int>

2013061200
2013061206
2013061212

因此,如果我检查list<int> 中的最大值,我会知道哪个是最新文件。

我的最终目标是通过执行OpenTxtFile(string fileName)打开最新的文件

因此,我有一个类似下面的函数:

private void OpenLatestFileInLocal()
{
    // Get list of fileName from local directory
    // Ouput fileList_str = (string){20130612_00 , 20130612_06, 20130612_12}
    List<string> fileList_str = Directory.EnumerateFiles(localDir, "*.txt", SearchOption.AllDirectories).Select(Path.GetFileName).ToList();

    foreach (string fileName in fileList_str)
    {
        fileLIst_int.Add(Convert.ToInt32(RemoveSpecialCharacters(fileName)));
    }
    // Ouput: fileList_int = (int){2013061200 , 2013061206, 2013061212}

    int Latest = fileLIst_int.Max(); //Output: Latest = 2013061212

    // Problem is here.. my OpenTextFile function need to pass the fileName in string
    // But I identify the latest file as an (int) as 2013061212
    // I need to know 2013061212 is actually 20130612_12.txt
    // so that, I can pass the fileName into OpenTxtFile(string fileName)

    OpenTxtFile(fileName_Latest);

}

private void OpenTextFile(string fileName)
{

   // this function will only open file based on string fileName in local directory

}

【问题讨论】:

    标签: c# winforms ftp ienumerable


    【解决方案1】:

    如果您已经在使用Linq,请填充anonymous class 来存储路径和解析日期(您还可以查看DateTime.ParseExact,而不是转到int):

    private void OpenLatestFileInLocal()
    {
        var latestFile = Directory
            .EnumerateFiles(localDir, "*.txt", SearchOption.AllDirectories)
            .Select(path => new { Path = path, Date = DateTime.ParseExact(
                Path.GetFileNameWithoutExtension(path), "yyyyMMdd_HH", 
                CultureInfo.InvariantCulture) })
            .OrderByDescending(df => df.Date)
            .Select(df => df.Path)
            .FirstOrDefault();
    
        if (latestFile != null)
        {
            OpenTxtFile(latestFile);
        }
    }
    

    【讨论】:

    • ftp服务器中的所有文件名都是根据ftp服务器的系统时间创建的。我根据localdir的文件名检查最新文件,但不是创建时间,因为我无法确认,当我从ftp服务器下载文件到localdir时,本地目录的创建时间与文件相同ftp 中的创建时间。一个
    • 这意味着,它可能会发生类似我首先下载20130612_12.txt 并且localdir 的文件创建时间是下午12:51。然后我下载20130612_00.txt,文件创建时间在localdir是下午12:52。如果最新文件是基于创建时间的,那么20130612_00.txt 将被识别为最新文件,这不是我想要的:)
    • @jhyap,我明白了。上面的代码(之前有一个错字)将文件名作为日期并按降序对其进行排序。关键行是DateTime.ParseExact(Path.GetFileNameWithoutExtension(path), "yyyyMMdd_HH", CultureInfo.InvariantCulture),它将采用路径,删除扩展名并尝试将其解析为具有所列格式的日期("yyyyMMdd_HH" - 4 位数年份,2 位数月份,2 位数日期,下划线,2 位数 24小时)。
    • 好的,我明白你的意思了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-05
    • 2020-07-14
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多