private void Fsw_Created(object sender, FileSystemEventArgs e)
{
if (!e.FullPath.Contains("$RECYCLE.BIN"))
{
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);
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);
// The copy part is not working good yet.
// when creating a new file it's saying the file have been copied.
// it should say file created only when creating a new file and not copied.
// it should say file copied now only when copying from other directory/ies.
/*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;
}
}
}*/
}
});
}
}
I can make either copied or created because if i'm using the copy part it will display a file copied when i'm creating a new file. and the other way if copy a file and using the created part.
i'm also not sure i'm using fine in the copy part to get the original source folder of the copied file to show the user where the file is copied from.
the variable dic in the copy part is a dictionary<string, long> type. and i'm using it first time like that :
private void Bgw_DoWork(object sender, DoWorkEventArgs e)
{
fileslist = GetFiles(getFilesString, "*.*", fswIncludeDirectoriesFlag).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);
}
}
}
}
what i'm trying to archive is when the user create a new file display something like :
"The file: Test.txt have been created in d:"
and if the user copy a file then display something like :
"The file: Test.txt have been copied from the folder D:\ to the folder D:\Testing12345"
Updating on my progress :
Ok, this is working fine for both cases. when i'm copying a file to another folder it will display that the file created in the other folder and also copied to the other folder.
if (!StringFromRichTextBox(RichTextBoxLogger).Contains(e.FullPath))
{
FileInfo info = new FileInfo(e.FullPath);
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);
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;
}
}
}
it's logic because the file copied to another folder and also the file created is now exist in the other folder.
I think the simplest way to detect if file have been copied or created is to check if the file have a destination folder. for example if i copied a file from D:\ to D:\Test\ then the file source folder is D:\ and the destination folder is D:\Test\ so that's mean the file have been copied.
if i created a file in D:\Test\ so the file source folder is D:\Test\ that's mean there is no destination folder only source folder.
with that logic i think i will be able to check if file have been copied or only created.
I will try it.