TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Hesham Hassanein
NA
140
17k
File System Watcher does not fire when more than one file
Oct 11 2016 7:53 AM
Hello,
I have a problem with File System Watcher, It does not fire when i create more than 1 file in the directory i am watching. It only works for 1 file, When 2 or more are copied into the directory at the same time. It does not work. It only sees 1 file.
I am doing 2 event handlers. 1 is onCreated and 1 is onChanged, In the Oncreated i add the files to a queue and call the function which does the process of the collected files and in OnChanged i delete one of the files in the list if it exists, If it does not exist, then do nothing.
What can be the problem in this code. Do i have to work with threading to make watcher sees this fast change in the directory or what?. Thanks in Advance. Here is the code
static
void
watch()
{
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher =
new
FileSystemWatcher();
watcher.Path = @
"Q:\New_Folder"
;
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter =
"*.DAT"
;
watcher.InternalBufferSize = 64000;
watcher.Created +=
new
FileSystemEventHandler(watcher_Created);
watcher.Changed +=
new
FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents =
true
;
Console.WriteLine(
"Watching Directory for New Files. To break the process Press \'q\'"
);
while
(Console.Read()!=
'q'
);
}
static
void
watcher_Created(
object
source, FileSystemEventArgs e)
{
// Specify what is done when a file is created
Console.WriteLine(
"File: "
+ e.FullPath +
" "
+ e.ChangeType);
SomeGlobalVariables.queuenames.Enqueue(e.FullPath);
// SomeGlobalVariables.filenameslist.ForEach(Console.WriteLine);
foreach
(
string
number
in
SomeGlobalVariables.queuenames)
{
Console.WriteLine(number);
}
foreach
(var filenames
in
SomeGlobalVariables.queuenames)
{
var fileName = filenames;
var Name_File = Path.GetFileName(fileName).Replace(
".DAT"
,
""
).Remove(0,6);
var tempfilepath = (@
"C:\Users\" + Name_File + "
.CSV");
Console.WriteLine(fileName);
ReadData(tempfilepath, fileName);
}
}
static
void
OnChanged(
object
source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed
//Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
if
(!SomeGlobalVariables.queuenames.Contains(e.FullPath))
{
Console.WriteLine(
"The file is not in the list"
);
Console.ReadKey();
}
else
if
(SomeGlobalVariables.queuenames.Contains(e.FullPath))
{
var last_item = SomeGlobalVariables.queuenames.Last();
SomeGlobalVariables.filenameslist.Remove(last_item);
Console.WriteLine(
"deleted"
);
foreach
(
string
number
in
SomeGlobalVariables.queuenames)
{
Console.WriteLine(number);
}
}
}
Reply
Answers (
4
)
ComboBox in Windows Phone 8.1
How to save scanned image c#