private void GetBackup_Click(object sender, EventArgs e)
{
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
if(System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);
foreach (string s in files)
{
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath,fileName);
System.IO.File.Copy(s,destFile,true);
}
MessageBox.Show("Backup process is complete!", "Taking a backup copy", MessageBoxButtons.OK);
}
else
{
Console.WriteLine("Source path does not exist!");
}
}
Eline SamiPosted Aug 20, 2014, 4:59 AM
Akhil KharePosted Aug 19, 2014, 3:23 AM
You are calling this runwatcher method once that why its executing only once. here some links which may help you in this context
http://www.dotnetbull.com/2013/06/filesystemwatcher-multiple-folder.html
http://www.codeproject.com/Articles/18521/How-to-implement-a-simple-filewatcher-Windows-serv
http://www.c-sharpcorner.com/uploadfile/puranindia/filesystemwatcher-in-C-Sharp/
and for your next question, yes filesystemwatcher will works only if application is running.. so you have two option, just create simple exe application and configure it as a task or (prefer) create a windows service and deploy it so it will run in background and OS will take care for resource.
Thanks
Akhil
Eline SamiPosted Aug 19, 2014, 2:07 AM
Hello Akhil
Thanks for the help. Well, I am interested in FileSystemWatcher Class to use, and therefore, I've checked and then wrote the following function.
But, it executes only the first if condition (in Bold) and skips the rest, Why is that?
and also, where to call the run Watcher method to keep checking on the files (inside a folder) without running my application each time manually?
See where I've tried to call in my code.
private void GetBackup_Click(object sender, EventArgs e)
{
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
if(System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);
foreach (string s in files)
{
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath,fileName);
System.IO.File.Copy(s,destFile,true);
}
MessageBox.Show("Backup process is complete!", "Taking a backup copy", MessageBoxButtons.OK);
runWatcher();
}
else
{
Console.WriteLine("Source path does not exist!");
}
}
private void runWatcher()
{
string[] args = System.Environment.GetCommandLineArgs();
//if a directory is not specified, exit program.
if (args.Length != 2)
{
//Display the proper way to call the program.
Console.WriteLine("Usage: Watcher.exe(directory)");
return;
}
//create a new FileSystemWatcher and set its properties:
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = args[1];
//Watch for changes in the lastAccess and LastWrite times and the renaming of files or directoreis//
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files..
watcher.Filter = "*.txt";
//add event handlers:
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
//begin watching
watcher.EnableRaisingEvents = true;
//watif for the user to quite the program.
Console.WriteLine("press \'q\' to quite the sample. ");
while (Console.Read() != 'q') ;
}
//Define the event hanlders.
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
}
Akhil KharePosted Aug 18, 2014, 8:27 AM
File system watcher class will help you to achieve this functionality, it will check every time if anything get changed into file then it notify and on notify you can make a copy that particular file.
Scheduled Task is one of the way to keep you application running, but you can also choose the windows service which runs in background.
Thanks
Akhil