⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0,1410,21743,00.html

📁 C++builder学习资料C++builder
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<HTML>



<HEAD>



<TITLE>Understanding Directory Monitoring</TITLE>

</HEAD>

<BODY MARGINWIDTH="5" MARGINHEIGHT="5" TOPMARGIN="0" LEFTMARGIN="0" BGCOLOR="#FFFFFF">



<A NAME="top"></A>



<table>

       



<TR>

   <td>

  

<SPAN CLASS="title3"><b>Understanding Directory Monitoring</b></SPAN> 

 		 

	<BR> 

 

    <P> 

<BLOCKQUOTE CLASS="abstract"><B>Abstract:</B>Creating a ListBox Component that will automatically monitor the contents of a directory and update itself when it changes.</BLOCKQUOTE><P> 

  

  </table> 

<a name="top"></a> 

<table width="100%" border=0 cellpadding=0 cellspacing=0> 

<tr valign=center><td align=middle> 

<font face="Arial">Using a Custom ListBox Component to Illustrate Directory Monitoring</font></td></tr>  

<tr><td align=left><br>  

Have you ever wondered how it is that shell windows and some applications can (almost magically, it seems)  

know when the contents of a directory has changed? (No, they don't sit there looping with an endless <b>if</b>  

statement!) The answer is actually quite simple: it is through the use of a function called   

<font face="MS Sans Serif">FindFirstChangeNotification</font>. The concept of it is simple enough, but the actual  

implementation takes a bit of explaining.<br><br>  

  

<table cellpadding="0" cellspacing="0" border="1">  

<a name="toc"><tr><td align=middle>Table Of Contents</td></tr></a>  

<tr><td><A style="text-decoration: none;" href="#objective" onMouseover="window.status='Objective';return true">Objective</a></td></tr>  

<tr><td><A style="text-decoration: none;" href="#concept" onMouseover="window.status='Concept';return true">Concept</a></td></tr>  

<tr><td><A style="text-decoration: none;" href="#impl" onMouseover="window.status='Implementation';return true">Implementation</a></td></tr>  

</table><br>  

</td></tr>  

  

<tr><td align = left>  

<a name="objective"></a><A style="text-decoration: none;" href="#toc"><font face="Arial">Objective</font></a> 

</td></tr> 

 

<tr><td><br> 

<big>T</big>he purpose of this article is to describe the concepts and implementation of directory monitoring. I will 

explain how it works and provide sample code for a custom component that uses it. I am assuming that you have 

a grasp of the following concepts:<br><br> 

 

<ul> 

	<li>Borland C++Builder  

         

	<li>Component Creation  

         

	<li>Win32 API</li> 

</ul><br> 

</td></tr> 

 

<tr><td align = left> 

<a name="concept"></a><A style="text-decoration: none;" href="#toc"><font face="Arial">Concept</font></a> 

</td></tr> 

 

<tr><td> 

<br> 

<P><big>T</big>he concept behind directory monitoring is fairly simple. As I said above, this can be done using the function 

<font face="MS Sans Serif">FindFirstChangeNotification</font>. That said and done, how do we go about actually  

using the thing? First lets look at the prototype, as given by Microsoft:</P> 

 

<P> 

<TABLE width=100% bgColor="#dddddd"> 

<TR><TD> 

<P><span style="font-family: "Courier New", Courier, monospace;font-size: 80%;"><STRONG>HANDLE</STRONG> FindFirstChangeNotification(<BR>&nbsp;   

<STRONG>LPCTSTR</STRONG> lpPathName,&nbsp;&nbsp;&nbsp; <EM>// directory name<BR>&nbsp; </EM>  

<STRONG>BOOL</STRONG> bWatchSubtree,&nbsp;&nbsp;&nbsp; <EM>// monitoring option<BR>&nbsp; </EM>  

<STRONG>DWORD</STRONG> dwNotifyFilter&nbsp;&nbsp; <EM>// filter conditions<BR></EM>);</span></P>  

</TD></TR>  

</TABLE>            

<br>  

<pre>

<font size=+1><b>Parameters</b></font>

 <b>lpPathName</b>: the path of the directory to monitor

 <b>bWatchSubtree</b>: whether or not to recursively monitor subdirectories

 <b>dwNotifyFilter</b>: flags to control monitoring conditions



The options available for dwNotifyFilter are as follows:</pre><br>  

<p align=center>  

<table width=92% border=0 cellspacing=3 cellpadding=10>  

<span style="font-family: "Courier New", Courier, monospace;font-size: 80%;">  

<tr>  

<td align="center" bgcolor=#c1c1c1><b>Value</b></td><td align="center" bgcolor=#clclcl><b>Meaning</b></td>  

</tr>  

<tr bgcolor="#e3e3e3">  

<td>FILE_NOTIFY_CHANGE_FILE_NAME</td>  

<td>Any file name change in the watched directory or subtree causes a change notification wait operation to return. Changes include renaming, creating, or deleting a file name.</td>  

</tr>  

<tr bgcolor="#e3e3e3">  

<td>FILE_NOTIFY_CHANGE_DIR_NAME</td>  

<td>Any directory-name change in the watched directory or subtree causes a change notification wait operation to return. Changes include creating or deleting a directory.</td>  

</tr>  

<tr bgcolor="#e3e3e3">  

<td>FILE_NOTIFY_CHANGE_ATTRIBUTES</td>  

<td>Any attribute change in the watched directory or subtree causes a change notification wait operation to return.</td>  

</tr>  

<tr bgcolor="#e3e3e3">  

<td>FILE_NOTIFY_CHANGE_SIZE</td>  

<td>Any file-size change in the watched directory or subtree causes a change notification wait operation to return. The operating system detects a change in file size only when the file is written to the disk. For operating systems that use extensive caching, detection occurs only when the cache is sufficiently flushed.</td>  

</tr>  

<tr bgcolor="#e3e3e3">  

<td>FILE_NOTIFY_CHANGE_LAST_WRITE</td>  

<td>Any change to the last write-time of files in the watched directory or subtree causes a change notification wait operation to return. The operating system detects a change to the last write-time only when the file is written to the disk. For operating systems that use extensive caching, detection occurs only when the cache is sufficiently flushed.</td>  

</tr>  

<tr bgcolor="#e3e3e3">  

<td>FILE_NOTIFY_CHANGE_SECURITY</td>  

<td>Any security-descriptor change in the watched directory or subtree causes a change notification wait operation to return.</td>  

</tr>  

</span>  

</table>  

  

<br>  

<p>The return value of this function is a windows HANDLE type. It gives us a Windows event object, which is an object  

that can be set to have a signalled state. Using this event handle, we can call the WaitForSingleObject or WaitForMultipleObjects  

functions to wait until it is signalled (the monitored directory has changed in some way). Once the event becomes signalled   

(i.e. a change was detected), it is necessary to call the <font face="MS Sans Serif">FindNextChangeNotification.</font>   

function to reset it. When you are finished, you must free the object with <font face="MS Sans Serif">FindCloseChangeNotification</font></p>  

<p>One way to utilize this function in a component is to put it within a thread with a loop that calls FindFirstChangeNotification,  

WaitForSingleObject, FindNextChangeNotification, and FindCloseChangeNotification. Every time the event object becomes signalled,  

we can call a function in the ListBox component that will notify it of change.</p>  

<br>  

</td></tr>  

<tr><td align = left>  

<a name="impl"></a><A style="text-decoration: none;" href="#toc"><font color=white face="Arial" size=+1>I</font><font face="Arial">mplementation</font></a> 

</td></tr> 

 

<tr><td> 

<br> 

<p><big>I</big>n implementing a directory monitoring and auto-updating file ListBox, there are a couple of things to keep in mind: 

how to monitor the directory without locking your application, and how to notify the control.One relatively simple  

way of accomplishing this is to create a thread that monitors the directory and calls back to the ListBox component  

when there is a change. In the following source, both code for a ListBox and for the monitoring thread are provided.</p> 

<p>Here is the code for the component:</p> 

<table style="font-family: "Courier New", Courier, monospace;font-size: 80%;" border=1 width=100% cellpadding=0 cellspacing=0> 

<tr bgcolor=blue><td><font color=white face="Arial">ActiveListBox.h</font></td></tr> 

<tr><td> 

<pre>

<font color="#0000AA"><i>//---------------------------------------------------------------------------</i></font>



<font color="202020">#ifndef ActiveListBoxH</font>

<font color="202020">#define ActiveListBoxH</font>

<font color="#0000AA"><i>//---------------------------------------------------------------------------</i></font>

<font color="202020">#include &lt;SysUtils.hpp&gt;</font>

<font color="202020">#include &lt;Controls.hpp&gt;</font>

<font color="202020">#include &lt;Classes.hpp&gt;</font>

<font color="202020">#include &lt;Forms.hpp&gt;</font>

<font color="202020">#include &lt;StdCtrls.hpp&gt;</font>

<font color="#0000AA"><i>//---------------------------------------------------------------------------</i></font>



<font color="#0000AA"><i>//Here we combine the flags into one easy-to-use dword</i></font>

<b>const</b> <b>unsigned</b> <b>long</b> dwFlags = FILE_NOTIFY_CHANGE_FILE_NAME|

                            FILE_NOTIFY_CHANGE_DIR_NAME|

                            FILE_NOTIFY_CHANGE_ATTRIBUTES|

                            FILE_NOTIFY_CHANGE_SIZE|

                            FILE_NOTIFY_CHANGE_LAST_WRITE;

<b>class</b> TActiveListBox;



<b>class</b> TDirectoryThread : <b>public</b> TThread

{

<b>private</b>:

  HANDLE hNotify;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000AA"><i>//Event object handle</i></font>

  TActiveListBox *control;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000AA"><i>//ListBox back pointer (to notify it)</i></font>

  String path;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000AA"><i>//Path to monitor</i></font>

  <b>void</b> <b>__fastcall</b> Update();

  <b>void</b> <b>__fastcall</b> ThreadFinish(TObject *Sender);

<b>protected</b>:

  <b>void</b> <b>__fastcall</b> Execute();

<b>public</b>:

  <b>__fastcall</b> TDirectoryThread(TActiveListBox *ctrl);

  <b>void</b> <b>__fastcall</b> SetMonitorPath(String path);

};

<font color="#0000AA"><i>//---------------------------------------------------------------------------</i></font>

<b>class</b> PACKAGE TActiveListBox : <b>public</b> TListBox

{

<b>private</b>:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -