本文介绍了如何从目录获取文件的最新列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我想使用asp.net从目录中检索文件的最新列表

hello

i want to retrive the latest list of file from directory using asp.net

推荐答案


public string SearchLatestFile(string path, string extension, string searchstring)
{
    DirectoryInfo di = null;
    if (Directory.Exists(path))
    {
        di = new DirectoryInfo(path);
    }
    else
        return "Directory Does not Exist";
    string newestFile;
      IEnumerable<system.io.fileinfo> fileList = di.GetFiles("*" + searchstring.ToLower() + "*");
    //Create the query
    IEnumerable<system.io.fileinfo> fileQuery =
        from file in fileList
        where (extension.ToLower().Contains(file.Extension.ToLower()))
        orderby file.LastWriteTime
        select file;

    //Execute the query. This might write out a lot of files!

    try
    {

        var FileSearchedResult = (from file in fileQuery orderby file.LastWriteTime select new { file.FullName, file.Name, file.CreationTime }).Last();
        newestFile = FileSearchedResult.FullName;
        FileSearchedResult = null;
        fileList = null;
        di = null;
        return newestFile;
    }
    catch
    {
        fileList = null;
        di = null;
        return null;

    }


}


这篇关于如何从目录获取文件的最新列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 06:05