1

I need monitor a folder, see if a file or files has been uploaded. And then I need to get the created date & time of the latest file that has been uploaded and see whether the time creation of the file has been more than 30 minutes from the current time. I have used the FileSystemWatcher to monitor the folder but how should I proceed for finding and comparing the latest file with current time.

private void watch()
{
  FileSystemWatcher watcher = new FileSystemWatcher();
  watcher.Path = path;
  watcher.NotifyFilter = NotifyFilters.LastWrite;
  NotifyFilters.DirectoryName;
  watcher.Filter = "*.*";
  watcher.Changed += new FileSystemEventHandler(OnChanged);
  watcher.EnableRaisingEvents = true;
}

Private void OnChanged(object source, FileSystemEventArgs e)
{
  //Copies file to another directory.
}

How shall I do that in c#. Please help!

velvt
  • 143
  • 1
  • 3
  • 13
  • look here for help on `FileSystemEventArgs` https://msdn.microsoft.com/en-us/library/system.io.filesystemeventargs(v=vs.110).aspx – GreatAndPowerfulOz Jul 06 '16 at 20:07
  • But `OnChanged` fires once the file is created (uploaded) => the creation time will be `Now`. Do you mean you want to check if the upload process took more than 30 minutes ? Or what exactly are you after ? – Zein Makki Jul 06 '16 at 20:08
  • @user3185569 Actualy im planning to run the code as a scheduled task to be run every 1 hour. so im expecting it to then see if the latest file created in the folder has been created more than 30 mins from now. – velvt Jul 06 '16 at 20:10
  • Does the question with this answer not help? http://stackoverflow.com/a/15018082/868127 – claudekennilol Jul 06 '16 at 20:11
  • @claudekennilol yes but only partly. How do i check the latest file created in the folder and compare the time created to be more than 30mins from now. – velvt Jul 06 '16 at 20:12
  • @velvt a scheduled task has nothing to do with `FileSystemWatcher`. The watcher notifies you for any changes in the directory. If you have a scheduled task to run every 1 hour, you can just read the directory and check the files. No need for a Watcher. – Zein Makki Jul 06 '16 at 20:13
  • cant you do something like `var halfHourAgo = DateTime.Now.AddMinutes(-30); var dir = Directory.CreateDirectory(@"C:\Temp"); var fileList = new List(); foreach (var fl in dir.GetFiles("*.*")) { if (fl.CreationTime < halfHourAgo) { fileList.Add(fl); } } var selected = fileList.OrderBy(c=> c.CreationTime).FirstOrDefault();` – Nilesh Jul 06 '16 at 20:40
  • I need to get only the oldest file in the directory (sort by created date) and compare this creation date if it is more than 30 mins from now. – velvt Jul 07 '16 at 08:57

2 Answers2

1

From your comments, i can't really see why you need to use FileSystemWatcher. You say you have a scheduled task every 1 hour that needs to check a directory for creation time of files. So in that task, just do the below:

// Change @"C:\" to your upload directory
string[] files = Directory.GetFiles(@"C:\");

var oldestFile = files.OrderBy(path => File.GetCreationTime(path)).FirstOrDefault();
if (oldestFile != null)
{
    var oldestDate = File.GetCreationTime(oldestFile);

    if (DateTime.Now.Subtract(oldestDate).TotalMinutes > 30)
    {
        // Do Something
    }
}

To Filter specific files, use the overload :

string[] files = Directory.GetFiles(@"C:\", "*.pdf");
Zein Makki
  • 29,485
  • 6
  • 52
  • 63
  • What if a need to filter a specific types of files, let say pdf? – velvt Jul 06 '16 at 20:19
  • Now if i need to access a directory that is not found locally on my computer but on a remote computer. Lets say @"\\testweb\checkfiles\"); But my computer doesnt have the permission to access that remote directory, i need to specify credentials of the authorised account user to do this. How should i proceed? – velvt Jul 06 '16 at 20:32
  • 1
    @velvt If it is accessible from the server you're executing the code on, go for it. If there are any further implications needed for network locations, then i think this is a different question - "How to access files in a network location using c#" and it is not directly related to this question "How to check from files created before 30 minutes ?". – Zein Makki Jul 06 '16 at 20:36
  • I need to get only the oldest file in the directory (sort by created date) and compare this creation date if it is more than 30 mins from now. – velvt Jul 07 '16 at 08:41
  • When their is no file in the folder, it crashes with the following exception 'System.ArgumentNullException' occurred in mscorlib.dll Additional information: Value cannot be null. – velvt Jul 07 '16 at 09:43
  • @velvt C'mon! That's a simple if statement. Check the update. – Zein Makki Jul 07 '16 at 09:45
1

In the OnChanged event:

    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        var currentTime = DateTime.Now;
        var file = new FileInfo(e.FullPath);
        var createdDateTime = file.CreationTime;
        var span = createdDateTime.Subtract(currentTime);

        if (span.Minutes > 30)
        {
            // your code
        }
    }

To filter on specific file extensions (such as pdf), you can use:

if (file.Extension == ".pdf")
{

}
William Xifaras
  • 5,212
  • 2
  • 19
  • 21