Click here to Skip to main content
15,915,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello

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

plz help me as soon as possible
Posted
Updated 25-Aug-12 21:59pm
v2

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
 
Share this answer
 
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).



C#
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;

    }


}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900