Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


500pts: File Comparing a directory?

Posted on 2006-11-23
Medium Priority
Last Modified: 2010-04-16
Hi there,

I wonder if anyone can help?

I am trying to find the easiest way to file compare a complete directory.. The reason for this is to find out if i need to send any changes in the directory to a Database BLOB field..

I have the blob field working... What i need to do is to take a Snapshot of all files when I enter my program... I thought of reading in the files into a array or something but a little unsure here...

During the use of the program files in a certain directroy may get updated or files maybe added or deleted.... so when i exit the program i will compare my previous Snapshot to the files now in the directory and if any have changed then i would send these files to the db.... or remove files from the db or add files to the db.....

Any help or info on this would be really helpful, i am a little lost the best way of doing this... If i keep the files in an array in memory this would be costly wouldn't it??

Thanks in advance

Question by:ianinspain
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 48

Assisted Solution

AlexFM earned 800 total points
ID: 18001775
Did you think about using FileSystemWatcher? Something like this:


Accepted Solution

redpipe earned 1200 total points
ID: 18002772
The FileSystemWatcher is very good when you need to trigger on events, as they happen. If you only want to compare two different snapshots, I would used a simple hashtable.

    public System.Collections.Hashtable Init(string path)
      System.Collections.Hashtable ht = new System.Collections.Hashtable();
      System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(path);
      foreach (System.IO.FileInfo fi in di.GetFiles()) {
        ht.Add(fi.Name, fi.LastWriteTime);

    public void Compare(string path, System.Collections.Hashtable htOld)
      System.Collections.Hashtable htCurrent = new System.Collections.Hashtable();
      System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(path);
      foreach (System.IO.FileInfo fi in di.GetFiles()) {
        if (!htOld.ContainsKey(fi.Name)) {//new file
          //TODO: Add code to add current file to database
      foreach (System.Collections.DictionaryEntry de in htOld.Keys) {
        System.IO.FileInfo fi = new System.IO.FileInfo(path + "\\" + de.Key.ToString());
        if (fi.Exists) {
          if (fi.LastWriteTime > Convert.ToDateTime(de.Value)) {
            //TODO: Add code to update current file to database
        } else { //deleted file
          //TODO: Add code to remove current file from database

Author Comment

ID: 18003647
hi redpipe, this i like... but i was wondering about the property


is this guaranteed to be always updated when a feel is changed??

Just wondering, otherwise it sounds a really good solution..

AlexFM, this is also a brilliant solution, i will probably keep this and implement elsewhere.... i never even knew it existed... thnaks for that

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 18004016
feel is supposed to read "file" :-)


Expert Comment

ID: 18006289
hi Ian

The "LastWriteTime" property is the same as the "Date Modified" column in Windows Explorer. As all properties with a getter and setter it is possible to programatically override the "correct" value (e.g. set the LastWriteTime to a date before the computer was invented :-), but if you just write your changes to the file through the normal System.IO methods, the property should be updated accordingly by the OS.

Author Comment

ID: 18029609
Thank you...

Much appreciated


Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question