Modified Files Finder in C#

Introduction

This article is helpful for all the developers who want a list of files on which they have worked for the recent/current task. (In case you are not using VSS.)

This article explains how to get the list of all the files that you have modified in the directory and subdirectory. This list is sorted in descending order of modified date.

The following is the procedure.

Step 1

Create a new empty ASP.NET web site using C# in Visual Studio.

On the default.aspx page use a grid view as follows:

  1. <asp:GridView id="gvModifiedFiles" runat="server" AutoGenerateColumns="true"></asp:GridView>  

Here I gave the GridView the Id "gvModifiedFiles".

Step 2

In the Default.aspx.cs page include the following namespace:

  1. using System.Data;  
  2. using System.IO;  
  3. using System.Linq;  

Declare and create the following objects globally above the page_Load method.

  1. DataView dv = new DataView();  
  2. DataTable dt = new DataTable();  
  3. DataRow dr;  

Step 3

In the web.config file add a key inside appsettings:

  1. <appSettings>  
  2.      <add key ="directoryPath" value="d:\ashwork\web_dev_og\src"/>  
  3. </appSettings>  

This will be your directory path in which you need to search for all the files with their modified dates.

Step 4

Then get the "directoryPath" value from web.config file on the Page_Load method as follows:

  1. string dirPath = ConfigurationManager.AppSettings["directoryPath"].ToString();   
  2. SearchDirectory(dirPath);  

Step 5

Then call the method SearchDirectory from pageload as shown above. Then write a method SearchDirectory as follows:

  1. protected void SearchDirectory(String folderName)  
  2. {  
  3.      DirectoryInfo DirInfo = new DirectoryInfo(folderName);  
  4.    
  5.      DateTime dt1 = new DateTime(2013, 01, 1);  
  6.      DateTime dt2 = DateTime.Now;  
  7.    
  8.      // LINQ query for files   
  9.      var files = from file in DirInfo.EnumerateFiles()  
  10.                      where file.CreationTimeUtc > dt1 & file.CreationTimeUtc < dt2  
  11.                      select file;  
  12.    
  13.      // Show results.  
  14.      foreach (var file in files)  
  15.      {  
  16.          if (file.Extension != ".dll" && file.Extension != ".xml" && file.Extension != ".pdb" && file.Extension != ".sln" && file.Extension != ".bak" && file.Extension != ".jpg" && file.Extension != ".scc" && file.Extension != ".cache" && file.Extension != ".txt" && file.Extension != ".suo" && file.Extension != ".vssscc" && file.Extension != ".XML" && file.Extension != ".csproj" && file.Extension != ".user" && file.Name != "AssemblyInfo.cs" && file.Extension != ".vspscc" && file.Extension != ".htm" && file.Extension != ".TMP" && file.Extension != ".pdf" && file.Extension != ".png" && file.Extension != ".gif" && file.Extension != ".settings" && file.Extension != ".xsd" && file.Extension != ".csdl" && file.Extension != ".msl" && file.Extension != ".ssdl" && file.Extension != ".mdf" && file.Extension != ".ldf" && file.Extension != ".zip" && file.Extension != ".JPG" && file.Extension != ".db" && file.Extension != ".swf" && file.Extension != ".cd")  
  17.          {  
  18.              dr = dt.NewRow();  
  19.              dr["DateModified"] = file.LastWriteTime;  
  20.              dr["FileName"] = file.DirectoryName + "\\" + file.Name;  
  21.              dt.Rows.Add(dr);  
  22.          }  
  23.      }  
  24.    
  25.      foreach (var dir in DirInfo.EnumerateDirectories())  
  26.      {  
  27.          SearchDirectory(folderName + @"\" + dir.Name);  
  28.      }  
  29. }  

In the preceding method we used the DirectoryInfo Class of the System.IO namespace. Then used a LINQ query to get all the files present in the folder. In the first foreach loop we prepared each datarow with datemodified and filename. Then added the datarow "dr" to the dataTable dt.

In the second foreach loop we called the SearchDirectory again for the next subFolder or subDirectory. In this way we added all the file names with the last modified date.

Step 6

Then finally in the page_load method after calling the SearchDirectory method, bind the GridView with the dataTable "dt" as follows:

  1. DataView dv = dt.DefaultView;  
  2. dv.Sort = "DateModified DESC";  
  3. gvModifiedFiles.DataSource = dv.Table.DefaultView;  
  4. gvModifiedFiles.DataBind();  

In the code above we are sorting the dataview and then binding the defaultView of the dataview with GridView.

After running this web page you can see that the output having all the files in the directory and subdirectories are listed with the date modified.

That's It. Happy Coding :)

Up Next
    Ebook Download
    View all
    Learn
    View all