0

我已经考虑了答案并将其添加到我的代码中,但是由于某种原因,即使文件的写入时间非常不同,它似乎也采用了名称中数字较高的那个,而不是最近写入的那个时间。

我之前发布了这个问题,但无法得到答案,现在我已经发布了我正在使用的完整代码,因为我对问题所在感到困惑,澄清:我的目标是通过一个目录和获取某些文件,某些文件可能具有相同的名称,因此在这种情况下,我需要确保只使用最新的文件,据我所知,我已经在我的代码中考虑了这一点,但它不起作用..非常感谢任何帮助

    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;
    }
}

}

4

1 回答 1

0

要获取文件(最新文件)的修改时间,请使用

FileInfo.LastWriteTime and FileInfo.LastWriteTimeUTC

然后您可以使用最新日期的文件。

于 2013-07-05T08:52:04.823 回答