Thursday, 19 September 2013

C# App Make a log of changes in a directory/subdirectories

C# App Make a log of changes in a directory/subdirectories

I'm creating an app that checks for changes in ALL the files in a
directory/subdirectories in the previous 24 hours. (or any other time
given).
I've seen lots of other codes but not all of them contain what I'm looking
for.
This is my code so far:
string myDirectory = Path.Combine(@"path");
var directory = new DirectoryInfo(myDirectory);
DateTime from_date = DateTime.Now.AddDays(-1);
DateTime to_date = DateTime.Now;
var files = directory.GetFiles().Where(file => file.LastWriteTime >=
from_date && file.LastWriteTime <= to_date).ToArray();
Console.WriteLine(files);
In the end it has to create a .txt file containing the changes: (name of
the files that were changed). I already got this code working.
FileStream filestream = new FileStream(@"path", FileMode.Create);
var streamwriter = new StreamWriter(filestream);
streamwriter.AutoFlush = true;
Console.SetOut(streamwriter);
Console.SetError(streamwriter);
The problem is that the code above only writes the most previous change in
the given directory. What I want is ALL the changes in 24 hours in the
given directory.
I am kind of new to this, and my goal is to keep the code nice and compact.
I am NOT, I repeat NOT, looking for FileSystemWatcher, I don't want my
server to stay on for 24 hours. I want to boot this program once a day,
make a log, close it.
If anyone can help me out with this, or at least give me something to
start with, I would very much appreciate it!

No comments:

Post a Comment