Using .NET FileSystemWatcher to listen for new files
Show older comments
Hi,
I want to write a simple program that runs continuously and listens for the FileSystemWatcher events i.e. a new file has appeared in a folder. When the file arrives I want to process that file in some way. The following is code that I have been playing with:
function ImageSorter
%Start listeners for file new notifications
fileObj = System.IO.FileSystemWatcher('C:\Dicom\');
fileObj.EnableRaisingEvents = true;
l1 = addlistener(fileObj, 'Created', @eventhandlerChanged);
%now I want the program to stop and listen for the events and not
% continue to the end.
h = figure('visible','off');
waitfor(h);
delete(l1);
end
function eventhandlerChanged(source,arg)
arg.FullPath
end
If I insert a break point at the end of the ImageSorter program then it works and the eventhandlerChanged function is called when a new file appears in the folder C:\DICOM.
How do I make the function stop running to the end and wait for the listening events?
Jonathan
1 Comment
Ashish Uthama
on 21 Jun 2011
how about putting in a 'pause'?
Accepted Answer
More Answers (1)
E
on 7 Apr 2016
Hi , Im trying to use this piece of code but i have no need in the button that creates new files so i commented the following lines:
uicontrol('Parent',figHdl,...
'Style','pushbutton',...
'String','Write a file to the watched path.',...
'Units','normalized',...
'Position',[0.1,0.6,0.5,0.2],...
'Callback',@cmdWriteFile);
and its corresponding callback:
function cmdWriteFile(varargin)
%create a simple text file in the watched folder
tName = tempname;
[~,tName] = fileparts(tName);
fName =fullfile(pathToWatch,[tName,'.txt']);
dlmwrite(fName,1:5);
end %cmdWriteFile
After doing so, the listener is not listening and onCahnge() is not running on change...
any ides how are my actions related to the listener?
Thanks!
1 Comment
Mr.Dong
on 25 Dec 2019
I have the same problem,so how you deal with it?
Categories
Find more on COM Component Integration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!