home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / howto / doc / watcher.aspx < prev    next >
Encoding:
Text File  |  2000-06-08  |  3.5 KB  |  99 lines

  1.  
  2. <!-- #include virtual="/quickstart/howto/include/header.inc" -->
  3.  
  4. <%
  5.     runsample = Server.MapPath("/quickstart/howto/samples/Services/FileSystemWatcher/Watcher")
  6.     viewsource = "/quickstart/howto/samples/Services/FileSystemWatcher/Watcher/Watcher.src"
  7.     icon = "/quickstart/images/Console.gif"
  8.     caption = "Watcher.exe"
  9. %>
  10.  
  11. <h4>How Do I...Watch file system changes?</h4>
  12.  
  13. <div class="indent" style="width:660"><font face="Tahoma" size="1">
  14.  
  15. <p>Use the FileSystemWatcher component
  16. to monitor a file system and react when changes to it occur. This makes it
  17. possible for you to quickly and easily launch business processes when certain
  18. files or directories are created, modified, or deleted. For example, suppose you
  19. and a group of coworkers are collaborating on a document that is stored on a
  20. shared directory on your server. Using the FileSystemWatcher component, you can
  21. easily program your application to watch for changes to the shared directory.
  22. When a change is detected, the component can run processing that notifies each
  23. of the group members via e-mail.</p>
  24.  
  25. <p>This sample illustrates how to
  26. use a FileSystemWatcher to watch for any changes, renaming, creation, and
  27. deletion of any files in the specified directory. The application takes the
  28. directory you wish to watch as the only argument.</p>
  29.  
  30. <p>Try running the sample as follows:</p>
  31.  
  32.     </font><font face="Courier New" color="Blue" size="1"><blockquote>
  33.         > Watcher.exe c:\
  34.     </blockquote></font><font face="Tahoma" size="1">
  35.  
  36. <p>Now, open the c:\ directory and try creating, modifying and deleting a file in the
  37. directory. Observe the sample application printing an appropriate message to the
  38. console window.</p>
  39.  
  40. <p>In its simplest form, using a FileSystemWatcher involves:</p>
  41.  
  42. <ol>
  43.  
  44. <li>Creating a new instance of the component and specifing the directory to watch:
  45.  
  46.     </font><font face="Courier New" color="Blue" size="1"><p>
  47.         FileSystemWatcher watcher = new FileSystemWatcher();<br>
  48.         watcher.Path= "c:\Foo";
  49.     </p></font><font face="Tahoma" size="1">
  50.  
  51. <li>Adding event handler[s]:
  52.  
  53.     </font><font face="Courier New" color="Blue" size="1"><p>
  54.         watcher.AddOnCreated(new FileSystemEventHandler(OnChanged));<br>
  55.         watcher.AddOnDeleted(new FileSystemEventHandler(OnChanged));
  56.         
  57.     </p></font><font face="Tahoma" size="1">
  58.  
  59. <li>Specifying what the application should do when a change occurs:
  60.  
  61.     </font><font face="Courier New" color="Blue" size="1"><p>
  62.         public static void OnChanged(Object source, FileSystemEventArgs e)<br>
  63.         {<br>
  64.             Console.Write("File: {0} {1}", e.FullPath, e.ChangeType.Format());<br>
  65.             Console.WriteLine();<br>
  66.         }
  67.     </p></font><font face="Tahoma" size="1">
  68.  
  69. <li>Enabling the component:
  70.  
  71.     </font><font face="Courier New" color="Blue" size="1"><p>
  72.         watcher.Enabled = true;
  73.     </p></font><font face="Tahoma" size="1">
  74.  
  75. </ol>
  76.  
  77. </font></div>
  78.  
  79. <h4>Example</h4>
  80.  
  81. <p>
  82. <div class="indent">
  83. <a target="_blank" href="<%=runsample%>">
  84. <img style="border-color:black" border=1 src="<%=icon%>"><br>
  85. </a>
  86. <div class="caption"><%=caption%></div><br>
  87. [<a target="_blank" href="<%=runsample%>">View Sample</a>] | 
  88. [<a target="_blank" href="/quickstart/util/srcview.aspx?path=<%=viewsource%>">View Source</a>]<p>
  89. </div>
  90.  
  91. <h4>Source Code</h4>
  92.  
  93. <div class="code">
  94. <xmp>
  95. <!-- #include virtual="/quickstart/howto/samples/Services/FileSystemWatcher/Watcher/Watcher.cs" -->
  96. </xmp>
  97. </div>
  98.  
  99. <!-- #include virtual="/quickstart/howto/include/footer.inc" -->