NLog.Targets.FileTarget.DeleteOldDateArchives C# (CSharp) Method

DeleteOldDateArchives() private method

Deletes archive files in reverse chronological order until only the MaxArchiveFiles number of archive files remain.
private DeleteOldDateArchives ( string pattern ) : void
pattern string The pattern that archive filenames will match
return void
        private void DeleteOldDateArchives(string pattern)
        {
            if (!ShouldDeleteOldArchives())
            {
                return;
            }

            string fileNameMask = ReplaceFileNamePattern(pattern, "*");
            string dirName = Path.GetDirectoryName(Path.GetFullPath(pattern));
            string dateFormat = GetArchiveDateFormatString(this.ArchiveDateFormat);

            if (dirName != null)
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(dirName);
                if (!directoryInfo.Exists)
                {
                    Directory.CreateDirectory(dirName);
                    return;
                }

#if SILVERLIGHT && !WINDOWS_PHONE
                var files = directoryInfo.EnumerateFiles(fileNameMask).OrderBy(n => n.CreationTime).Select(n => n.FullName);
#else
                var files = directoryInfo.GetFiles(fileNameMask).OrderBy(n => n.CreationTime).Select(n => n.FullName);
#endif
                List<string> filesByDate = new List<string>();

                foreach (string nextFile in files)
                {
                    string archiveFileName = Path.GetFileName(nextFile);
                    int lastIndexOfStar = fileNameMask.LastIndexOf('*');

                    if (lastIndexOfStar + dateFormat.Length <= archiveFileName.Length)
                    {
                        string datePart = archiveFileName.Substring(lastIndexOfStar, dateFormat.Length);
                        DateTime fileDate = DateTime.MinValue;
                        if (DateTime.TryParseExact(datePart, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out fileDate))
                        {
                            filesByDate.Add(nextFile);
                        }
                    }
                }

                EnsureArchiveCount(filesByDate);
            }
        }
#endif