我已经将答案考虑在内并将其添加到我的代码中但是由于某种原因,即使文件的写入时间差别很大,它似乎也会使用名称中数字较大的数字而不是带有数字的数字。最近的写作时间。
我之前发布了这个问题,但无法得到答案,现在我已经发布了我正在使用的完整代码,因为我很难解决问题所在,要澄清:我的目标是彻底解决一个目录并获取某些文件,一些文件可能具有相同的名称,所以在这种情况下我需要确保只采用最新的文件,据我所知,我已经在我的代码中考虑了这一点,但它不会工作..任何帮助非常感谢
DirectoryInfo directoryInfo = new DirectoryInfo(@"D:\DevP\Custom");
/// Returns latest writen file from the specified directory.
/// If the directory does not exist or doesn't contain any file, DateTime.MinValue is returned.
/// <param name="directoryInfo">Path of the directory that needs to be scanned</param>
/// <returns></returns>
private static DateTime GetLatestWriteTimeFromFileInDirectory(DirectoryInfo directoryInfo)
{
if (directoryInfo == null || !directoryInfo.Exists)
return DateTime.MinValue;
FileInfo[] files = directoryInfo.GetFiles();
DateTime lastWrite = DateTime.MinValue;
foreach (FileInfo file in files)
{
if (file.LastWriteTime > lastWrite)
{
lastWrite = file.LastWriteTime;
}
}
return lastWrite;
}
/// Returns latest writen timestamp file from the specified directory.
/// If the directory does not exist or doesn't contain any file, DateTime.MinValue is returned.
private static FileInfo GetLatestWritenFileFileInDirectory(DirectoryInfo directoryInfo)
{
if (directoryInfo == null || !directoryInfo.Exists)
return null;
FileInfo[] files = directoryInfo.GetFiles();
DateTime lastWrite = DateTime.MinValue;
FileInfo lastWritenFile = null;
foreach (FileInfo file in files)
{
if (file.Name.StartsWith("GLA"))
{
if (file.LastWriteTime > lastWrite)
{
lastWrite = file.LastWriteTime;
lastWritenFile = file;
}
}
else if (file.Name.StartsWith("M&M"))
{
if (file.LastWriteTime > lastWrite)
{
lastWrite = file.LastWriteTime;
lastWritenFile = file;
}
}
else if (file.Name.StartsWith("TRIAL"))
{
if (file.LastWriteTime > lastWrite)
{
lastWrite = file.LastWriteTime;
lastWritenFile = file;
}
}
}
return lastWritenFile;
}
public void Main()
{
String keywordHorizon = ((String)Dts.Variables["User::KeywordGLA"].Value).ToUpper();
string filename = Dts.Variables["User::InputFile"].Value.ToString();
string checkname = filename.ToUpper();
if (Equals("CurrentProcess.arc", Dts.Variables["User::InputFile"].Value) == false)
{
String FullFilePath = filename;
int filecounter = 0; // Increment for counting files, if found
FileInfo fileinfo;
fileinfo = new FileInfo(FullFilePath);
if (checkname.Contains(keywordeHorizon))
{
Dts.Variables["User::GLAFile"].Value = FullFilePath;
filecounter = 1;
if (fileinfo.Exists)
{
if (fileinfo.Length == 0)
{
Dts.Variables["User::FileHasNoData"].Value = true;
Dts.Variables["User::PackageStatus"].Value = "ERROR";
Dts.Variables["User::Succeeded"].Value = false;
}
else
GetLatestWritenFileFileInDirectory(directoryInfo);
}
}
else if (checkname.Contains(keywordM&M))
{
Dts.Variables["User::M&Mfile"].Value = FullFilePath;
filecounter = 1;
if (fileinfo.Exists)
{
if (fileinfo.Length == 0)
{
Dts.Variables["User::FileHasNoData"].Value = true;
Dts.Variables["User::PackageStatus"].Value = "ERROR";
Dts.Variables["User::Succeeded"].Value = false;
}
else
GetLatestWritenFileFileInDirectory(directoryInfo);
}
}
else if (checkname.Contains(keywordTRIAL))
{
Dts.Variables["User::TRIALFile"].Value = FullFilePath;
filecounter = 1;
if (fileinfo.Exists)
{
if (fileinfo.Length == 0)
{
Dts.Variables["User::FileHasNoData"].Value = true;
Dts.Variables["User::PackageStatus"].Value = "ERROR";
Dts.Variables["User::Succeeded"].Value = false;
}
else
GetLatestWritenFileFileInDirectory(directoryInfo);
}
}
Dts.Variables["User::FileCount"].Value = (int)Dts.Variables["User::FileCount"].Value + filecounter;
}
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
答案 0 :(得分:0)
要获得修改文件(最新文件)的时间,请使用
FileInfo.LastWriteTime and FileInfo.LastWriteTimeUTC
然后你可以拿到最新日期的文件。