Как вернуть самый новый файл в каталоге в виде строки? - PullRequest
0 голосов
/ 21 июля 2011

Я хотел бы иметь возможность вернуть новый элемент (который был создан) в виде строки в программе

например. s = test.txt

каталог "download"

text.txt Date created 4/5/2011
something.txt Date created 1/1/2011

Ответы [ 3 ]

4 голосов
/ 21 июля 2011

Как насчет

Directory.EnumerateFiles("directory").
  OrderBy(f => File.GetCreationTime(f)).Last()
3 голосов
/ 21 июля 2011
string res = Directory.EnumerateFiles(direcory)
    .OrderByDescending(f => new FileInfo(f).CreationTime).FirstOrDefault();
0 голосов
/ 21 июля 2011

На основе фрагмента кода из MSDN

string startFolder = @"c:\Download\";
// Take a snapshot of the file system.
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder);

// This method assumes that the application has discovery permissions
// for all folders under the specified path.
IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);

//Create the query
IEnumerable<System.IO.FileInfo> fileQuery =
    from file in fileList
    where file.Extension == ".txt"
    orderby file.Name
    select file;

// Create and execute a new query by using the previous 
// query as a starting point. fileQuery is not 
// executed again until the call to Last()
var newestFile =
    (from file in fileList
     orderby file.CreationTime
     select new { file.FullName, file.CreationTime })
    .Last();

Console.WriteLine("\r\nThe newest .txt file is {0}. Creation time: {1}",
    newestFile.FullName, newestFile.CreationTime);

// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit");
Console.ReadKey();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...