Monday, March 3, 2014

How to check if files created on specific time in C#?

Hi


You can use a control called FileSystemWatcher to monitor files (created, changed, deleted, renamed).


Here is sample code for your reference



using System;
using System.IO;
using System.Windows.Forms;

namespace Test
{
public partial class FileTimeTest : Form
{
FileSystemWatcher fsw = null;

public FileTimeTest()
{
InitializeComponent();
}

private void FileTimeTest_Load(object sender, EventArgs e)
{
fsw = new FileSystemWatcher(@"D:\test"); // specify the folder to monitor....
fsw.Created += new FileSystemEventHandler(fsw_Created);
fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
fsw.EnableRaisingEvents = true;
fsw.SynchronizingObject = this;
}

void fsw_Created(object sender, FileSystemEventArgs e)
{
FileInfo fi = new FileInfo(e.FullPath);
if (fi.CreationTime.Date == DateTime.Today) // replace DateTime.Today with your scheduled time...
MessageBox.Show(e.Name);
}
}
}


No comments:

Post a Comment