Using filesystemwatcher how cam i detect when a file/directory have been copied and to display to the user the file/directory name and where it was copied from to ?
I tried in the create event first :
private void Fsw_Created(object sender, FileSystemEventArgs e)
{
string time = DateTime.Now.ToString("h:mm:ss tt");
if (DateTime.Now.Subtract(_lastTimeFileWatcherEventRaised).TotalMilliseconds < 100)
{
return;
}
_lastTimeFileWatcherEventRaised = DateTime.Now;
Dispatcher.Invoke(() =>
{
if (!StringFromRichTextBox(RichTextBoxLogger).Contains(e.FullPath))
{
FileInfo info = new FileInfo(e.FullPath);
if (File.Exists(e.FullPath))
{
foreach (var key in dic.Keys)
{
if (key.Contains(System.IO.Path.GetFileName(e.FullPath)))
{
TextRange rangeOfText2 = new TextRange(RichTextBoxLogger.Document.ContentEnd, RichTextBoxLogger.Document.ContentEnd);
rangeOfText2.Text = "\r" + "The File " + System.IO.Path.GetFileName(e.FullPath) +
" Copied From " + System.IO.Path.GetDirectoryName(key)
+ " To " + System.IO.Path.GetDirectoryName(e.FullPath);
rangeOfText2.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.LightCyan);
rangeOfText2.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
break;
}
}
}
}
});
}
The problem in this case is that if i create a new file it will account it like a copy and not created file. i want that if i create a new file show a message of the created new file with the directory name where it was created. and if i make a copy of a file display the file name where it was copied from and where to.
The variable dic is dictionary :
The dic variable at the top :
List<string> fileslist = new List<string>();
Dictionary<string, long> dic = new Dictionary<string, long>();
and then how i'm using with it first time :
private void Bgw_DoWork(object sender, DoWorkEventArgs e)
{
fileslist = GetFiles(@"D:\", "*.*", fsw.IncludeSubdirectories).ToList();
if (bgw.CancellationPending == true)
{
e.Cancel = true;
return;
}
if (fileslist.Count > 0)
{
bgw.ReportProgress(fileslist.Count);
for (int i = 0; i < fileslist.Count; i++)
{
if (bgw.CancellationPending == true)
{
e.Cancel = true;
return;
}
FileInfo info = new FileInfo(fileslist[i]);
if (File.Exists(info.FullName))
{
dic.Add(fileslist[i], info.Length);
}
}
}
}
And last the changed event :
private void Fsw_Changed(object sender, FileSystemEventArgs e)
{
if (DateTime.Now.Subtract(_lastTimeFileWatcherEventRaised).TotalMilliseconds < 100)
{
return;
}
_lastTimeFileWatcherEventRaised = DateTime.Now;
if (e.ChangeType != WatcherChangeTypes.Changed)
{
return;
}
else
{
FileAttributes attr = File.GetAttributes(e.FullPath);
if (!attr.HasFlag(FileAttributes.Directory))
{
var info = new FileInfo(e.FullPath);
var newSize = info.Length;
if (dic.ContainsKey(e.FullPath))
{
var oldsize = dic[e.FullPath];
var size = SizeSuffix(newSize);
var oldies = SizeSuffix(oldsize);
if (!StringFromRichTextBox(RichTextBoxLogger).Contains(e.FullPath) && newSize != oldsize)
{
Dispatcher.Invoke(() =>
{
TextRange rangeOfText1 = new TextRange(RichTextBoxLogger.Document.ContentEnd, RichTextBoxLogger.Document.ContentEnd);
rangeOfText1.Text = "\rFile " + e.Name + " Size : " + oldies + " Changed To New Size : " + size;
rangeOfText1.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.LightBlue);
rangeOfText1.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
});
newSize = oldsize;
}
dic[e.FullPath] = info.Length;
}
else
{
dic.Add(e.FullPath, info.Length);
}
}
}
}
Everything is working so far except the copy files part.
I'm not sure if the copy file/s folder/s should be in the created or changed event ? and how to do it ? i want to display to the user when a new file created or a new folder and also to display to the user when a file/folder have been copied to another folder to display the file/folder name and where it was copied from and where to.
In the created i could do only this but i want also to make a copy not only created :
TextRange rangeOfText1 = new TextRange(RichTextBoxLogger.Document.ContentEnd, RichTextBoxLogger.Document.ContentEnd);
rangeOfText1.Text = "\r" + e.Name + " Created At : " + time;
rangeOfText1.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.LightCyan);
rangeOfText1.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
i can make in the Created event either created new file or copied but i can't make it to be together and that it will display if created new one or if copied.