首先,我在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
}
答案 0 :(得分:1)
如果您已经在使用Linq
,请填充anonymous class以存储路径和解析日期(您还可以查看DateTime.ParseExact
而不是{{1} }}):
int