home *** CD-ROM | disk | FTP | other *** search
-
- <!-- #include virtual="/quickstart/howto/include/header.inc" -->
-
- <%
- runsample = Server.MapPath("/quickstart/howto/samples/Services/FileSystemWatcher/Watcher")
- viewsource = "/quickstart/howto/samples/Services/FileSystemWatcher/Watcher/Watcher.src"
- icon = "/quickstart/images/Console.gif"
- caption = "Watcher.exe"
- %>
-
- <h4>How Do I...Watch file system changes?</h4>
-
- <div class="indent" style="width:660"><font face="Tahoma" size="1">
-
- <p>Use the FileSystemWatcher component
- to monitor a file system and react when changes to it occur. This makes it
- possible for you to quickly and easily launch business processes when certain
- files or directories are created, modified, or deleted. For example, suppose you
- and a group of coworkers are collaborating on a document that is stored on a
- shared directory on your server. Using the FileSystemWatcher component, you can
- easily program your application to watch for changes to the shared directory.
- When a change is detected, the component can run processing that notifies each
- of the group members via e-mail.</p>
-
- <p>This sample illustrates how to
- use a FileSystemWatcher to watch for any changes, renaming, creation, and
- deletion of any files in the specified directory. The application takes the
- directory you wish to watch as the only argument.</p>
-
- <p>Try running the sample as follows:</p>
-
- </font><font face="Courier New" color="Blue" size="1"><blockquote>
- > Watcher.exe c:\
- </blockquote></font><font face="Tahoma" size="1">
-
- <p>Now, open the c:\ directory and try creating, modifying and deleting a file in the
- directory. Observe the sample application printing an appropriate message to the
- console window.</p>
-
- <p>In its simplest form, using a FileSystemWatcher involves:</p>
-
- <ol>
-
- <li>Creating a new instance of the component and specifing the directory to watch:
-
- </font><font face="Courier New" color="Blue" size="1"><p>
- FileSystemWatcher watcher = new FileSystemWatcher();<br>
- watcher.Path= "c:\Foo";
- </p></font><font face="Tahoma" size="1">
-
- <li>Adding event handler[s]:
-
- </font><font face="Courier New" color="Blue" size="1"><p>
- watcher.AddOnCreated(new FileSystemEventHandler(OnChanged));<br>
- watcher.AddOnDeleted(new FileSystemEventHandler(OnChanged));
-
- </p></font><font face="Tahoma" size="1">
-
- <li>Specifying what the application should do when a change occurs:
-
- </font><font face="Courier New" color="Blue" size="1"><p>
- public static void OnChanged(Object source, FileSystemEventArgs e)<br>
- {<br>
- Console.Write("File: {0} {1}", e.FullPath, e.ChangeType.Format());<br>
- Console.WriteLine();<br>
- }
- </p></font><font face="Tahoma" size="1">
-
- <li>Enabling the component:
-
- </font><font face="Courier New" color="Blue" size="1"><p>
- watcher.Enabled = true;
- </p></font><font face="Tahoma" size="1">
-
- </ol>
-
- </font></div>
-
- <h4>Example</h4>
-
- <p>
- <div class="indent">
- <a target="_blank" href="<%=runsample%>">
- <img style="border-color:black" border=1 src="<%=icon%>"><br>
- </a>
- <div class="caption"><%=caption%></div><br>
- [<a target="_blank" href="<%=runsample%>">View Sample</a>] |
- [<a target="_blank" href="/quickstart/util/srcview.aspx?path=<%=viewsource%>">View Source</a>]<p>
- </div>
-
- <h4>Source Code</h4>
-
- <div class="code">
- <xmp>
- <!-- #include virtual="/quickstart/howto/samples/Services/FileSystemWatcher/Watcher/Watcher.cs" -->
- </xmp>
- </div>
-
- <!-- #include virtual="/quickstart/howto/include/footer.inc" -->