Using .NET FileSystemWatcher to listen for new files

35 ビュー (過去 30 日間)
Jonathan Sykes
Jonathan Sykes 2011 年 6 月 21 日
コメント済み: Mr.Dong 2019 年 12 月 25 日
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 件のコメント
Ashish Uthama
Ashish Uthama 2011 年 6 月 21 日
how about putting in a 'pause'?

サインインしてコメントする。

採用された回答

Matthew Whitaker
Matthew Whitaker 2011 年 6 月 21 日
Jonathon, Here is an example of how to do it. Note you need to listen on the changed event and not the created event
function FileWatch(pathToWatch)
txtDetectedFiles = createFigure;
fileObj = System.IO.FileSystemWatcher(pathToWatch);
fileObj.EnableRaisingEvents = true;
changeListener =addlistener(fileObj, 'Changed', @onChange); %need to keep in scope
function txtDetectedFiles = createFigure
figHdl = figure('Name','FileWatcher',...
'Menubar','none',...
'Toolbar','none',...
'NumberTitle','off',...
'Units','normalized',...
'Position',[0.4,0.4,0.4,0.2]);
uicontrol('Parent',figHdl,...
'Style','text',...
'Units','normalized',...
'Position',[0.1,0.9,0.8,0.08],...
'String',['Path Watched: ',pathToWatch]);
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);
txtDetectedFiles = uicontrol('Parent',figHdl,...
'Style','edit',...
'Enable','inactive',...
'Units','normalized',...
'Position',[0.1,0.05,0.8,0.45],...
'Max',3);
end %createFigure
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
function onChange(~,evt)
existStr = get(txtDetectedFiles,'String');
if isempty(existStr)
existStr = {};
end %if
existStr{length(existStr)+1} = ['New file Detected: ',char(evt.FullPath.ToString())];
set(txtDetectedFiles,'String',existStr);
end %onChange
end %FileWatch
  3 件のコメント
Matthew Whitaker
Matthew Whitaker 2011 年 7 月 1 日
Great , glad it helped! Can you set that you accept the answer?
Thanks
Matt
Jagatpreet
Jagatpreet 2013 年 3 月 13 日
Your example was of great help. It provides me the answer to my two questions:
How can GUI be used while listening events
How to watch file system and send notification when new file is added programmatically.
Previously I was watching the file using timer function Thanks a lot

サインインしてコメントする。

その他の回答 (1 件)

E
E 2016 年 4 月 7 日
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 件のコメント
Mr.Dong
Mr.Dong 2019 年 12 月 25 日
I have the same problem,so how you deal with it?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by