本文整理汇总了C#中System.IO.DirectoryInfo.OrderByDescending方法的典型用法代码示例。如果您正苦于以下问题:C# DirectoryInfo.OrderByDescending方法的具体用法?C# DirectoryInfo.OrderByDescending怎么用?C# DirectoryInfo.OrderByDescending使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.DirectoryInfo
的用法示例。
在下文中一共展示了DirectoryInfo.OrderByDescending方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CheckFolderExists
/// <summary>
/// Проверка существования папки для хранения лог-файлов
/// </summary>
private static void CheckFolderExists()
{
//Генерируем путь для лог-файлов и кешируем в перменную FolderPath
FolderPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), //%user%/AppData/Roaming
"Gemino/Logs" //Папка лог-файлов
);
//если папка еще не создана
if (!Directory.Exists(FolderPath)) {
//создаем по кешированному пути
Directory.CreateDirectory(FolderPath);
} else { //если создана
//получаем список файлов
var files = new DirectoryInfo(FolderPath).GetFiles();
//сортируем по дате создания и пропускаем первые 3 файла
foreach (var file in files.OrderByDescending(x => x.CreationTime).Skip(3))
//4-й файл удаляется как самый старый
file.Delete();
}
}
示例2: getAllFiles
/// <summary>
/// Gets all files from the specified folder
/// </summary>
/// <param name="folder">which folder</param>
/// <param name="order">sort order</param>
/// <returns>list of all files</returns>
private List<FileInfo> getAllFiles(String folder, SortOrder order)
{
var retVal = new List<FileInfo>();
if (Directory.Exists(folder))
{
var fileInfo = new DirectoryInfo(folder).GetFiles();
switch (order)
{
case SortOrder.DateDescending:
retVal = fileInfo.OrderByDescending(f => f.LastWriteTime).ToList();
break;
case SortOrder.DateAscending:
retVal = fileInfo.OrderBy(f => f.LastWriteTime).ToList();
break;
case SortOrder.AtoZ:
retVal = fileInfo.OrderBy(f => f.Name).ToList();
break;
case SortOrder.ZtoA:
retVal = fileInfo.OrderByDescending(f => f.Name).ToList();
break;
default:
retVal = fileInfo.ToList();
break;
}
}
return retVal;
}
示例3: GetPreviousDataFile
/// <summary>
/// Gets the previous data file.
/// </summary>
/// <param name="name">The name.</param>
/// <returns></returns>
/// <exception cref="System.IO.FileNotFoundException">
/// Attempts to locate the newest file in the list of parsed data files
/// returned null.
/// </exception>
private static FileSystemInfo GetPreviousDataFile(string name)
{
Debug.Assert(!string.IsNullOrWhiteSpace(name), "The file name cannot be null or empty.");
string searchPattern = string.Format("{0}*", name);
IEnumerable<FileSystemInfo> fileInfo = new DirectoryInfo(DataPath).GetFileSystemInfos(searchPattern, SearchOption.TopDirectoryOnly);
if (!fileInfo.Any())
{
return null;
}
// Get the newest file in the list
FileSystemInfo newestFile = fileInfo.OrderByDescending(f => f.CreationTime).FirstOrDefault();
if (newestFile == null)
{
throw new FileNotFoundException("Attempts to locate the newest file in the list of parsed data files returned null.");
}
return newestFile;
}
示例4: updateLatestIntelFiles
private void updateLatestIntelFiles()
{
if (LastIntelReported > (DateTime.Now.AddMilliseconds(-1 * timerFileDiscover.Interval))) return; //If intel has been reported recently, we don't need to recheck.
if (DateTime.UtcNow.TimeOfDay > new TimeSpan(10, 59, 00) && DateTime.UtcNow.TimeOfDay < new TimeSpan(11, 05, 00))
{
setState(STATE.DOWNTIME);
appendText("Downtime Detected. Waiting for new chat logs to be created.");
}
if (state == STATE.RUNNING)
{
appendVerbose("Sending heartbeat.");
ReportIntel(string.Empty, "Running");
}
appendVerbose("Updating chatlog file list.");
string oldfiles = string.Empty;
foreach (FileInfo fi in roomToFile.Values)
oldfiles += fi.Name + ", ";
string report = string.Empty;
foreach (String roomName in Configuration.RoomsToMonitor)
{
Debug.WriteLine("KIU Checking for : " + roomName);
FileInfo[] files = new DirectoryInfo(Configuration.LogDirectory)
.GetFiles(roomName + "_*.txt", SearchOption.TopDirectoryOnly);
FileInfo fi = files.OrderByDescending(f => f.LastWriteTime).FirstOrDefault();
if (fi == null)
{
continue;
}
Debug.WriteLine("KIU Latest: " + fi);
// Check if eve has opened this file -> Eve is running and user has joined channel
Boolean inUse = false;
try
{
FileStream fs = fi.Open(FileMode.Open, FileAccess.Read, FileShare.None);
fs.Close();
}
catch
{
inUse = true;
}
if (!inUse)
{
Debug.WriteLine("KIU Skipping: " + fi);
continue;
}
Debug.WriteLine("KIU Using: " + fi);
roomToFile[roomName] = fi;
report += fi.Name + "\r\n";
}
// Clear offset list of old files if necessary.
List<FileInfo> deletethese = new List<FileInfo>();
foreach (FileInfo fi in fileToOffset.Keys)
if (!roomToFile.ContainsValue(fi)) deletethese.Add(fi);
foreach (FileInfo fi in deletethese)
fileToOffset.Remove(fi);
// If a new file is created, recheck to make sure we have the most up to date log files.
FileSystemWatcher watcher = new FileSystemWatcher(Configuration.LogDirectory);
watcher.NotifyFilter = NotifyFilters.CreationTime;
watcher.Created += new FileSystemEventHandler(FileCreated);
watcher.EnableRaisingEvents = true;
string newfiles = string.Empty;
foreach (FileInfo fi in roomToFile.Values)
newfiles += fi.Name + ", ";
if (!newfiles.Equals(oldfiles))
{
if (newfiles.Length > 2) newfiles = newfiles.Substring(0, newfiles.Length - 2); // trim the last comma and space
if (oldfiles.Length > 2) oldfiles = oldfiles.Substring(0, oldfiles.Length - 2); // trim the last comma and space
if (state == STATE.RUNNING || state == STATE.DOWNTIME) appendText(string.Format("Intel Files Changed. Old Files: {0}, New Files: {1}", oldfiles, newfiles));
if (state == STATE.DOWNTIME) setState(STATE.START);
}
lblMonitoringFiles.Invoke(new MethodInvoker(() => lblMonitoringFiles.Text = report));
}