如何从目录获取文件的最新列表 [英] How to Get Latest List of file from directory

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

问题描述

你好

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

plz尽快帮助我

hello

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

plz help me as soon as possible

推荐答案



当您引用ASP.NET时,这意味着它只能从Server文件夹中获取最新文件.您无法访问客户端计算机的文件夹.

如果要从服务器读取特定文件夹,可能有两种情况,
1)您想从特定目录中获取所有文件(并不意味着添加更多文件时会收到通知)

这是获取目录文件的很好的例子.
从特定目录中获取所有文件 [有关FileSystemWatcher的MSDN信息 [
Hi,

When you refer ASP.NET it means it can get latest files from Server folder only. you can not access folder of client computer.

If you want to read specific folder from the server there may be two scenario,
1) you want to get all files from specific directory(it doesn''t mean you got notification when some more files are added)

Here is very good examples of getting directory files.
Get all files from specific directory[^]

2) You want notification when new files are added or updated for specific folder.

Please Read MSDN information on FileSystemWatcher[^]

Hope this works for you
Thanks
-Amit Gajjar


以下方法将为您获取最新文件.我使用Linq方法的.Last()来获取最新的方法.通过对ileinfo属性的LastWriteTime进行排序获得的最新文件ID,并取最后一个.但是,如果要获取最新的前十个文件或其他文件,请按LastWriteTime和Take(10)的顺序排序.



The following method will get you latest file. I used .Last() of Linq method to get latest one. The latest files id obtained by ordering LastWriteTime of ileinfo propery and take last one. But if you want latest Top ten files or something, order by descending on LastWriteTime and Take(10).



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;

    }


}


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆