qr code generator asp net c# Exercise 2: Watch for Changes in the File System in Visual C#.NET

Paint QR Code ISO/IEC18004 in Visual C#.NET Exercise 2: Watch for Changes in the File System

2
QR Code JIS X 0510 Generator In Visual C#
Using Barcode creation for .NET framework Control to generate, create Denso QR Bar Code image in .NET applications.
www.OnBarcode.com
Quick Response Code Reader In Visual C#
Using Barcode decoder for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Input/Output (I/O)
Make Barcode In C#
Using Barcode generator for VS .NET Control to generate, create bar code image in .NET applications.
www.OnBarcode.com
Scanning Bar Code In C#
Using Barcode decoder for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
5. Within the ShowDirectory method, iterate through each subdirectory and call the ShowDirectory method. Doing this will call the ShowDirectory recursively to find all the files for every directory. This code might look something like this:
QR Code ISO/IEC18004 Printer In .NET Framework
Using Barcode encoder for ASP.NET Control to generate, create QR-Code image in ASP.NET applications.
www.OnBarcode.com
Draw QR Code 2d Barcode In .NET
Using Barcode encoder for VS .NET Control to generate, create QR Code image in VS .NET applications.
www.OnBarcode.com
' VB ' Go through subdirectories ' recursively Dim subDir As DirectoryInfo For Each subDir In dir.GetDirectories() ShowDirectory(subDir) Next // C# // Go through subdirectories // recursively foreach (DirectoryInfo subDir in dir.GetDirectories()) { ShowDirectory(subDir); }
Quick Response Code Creation In VB.NET
Using Barcode printer for VS .NET Control to generate, create QR-Code image in .NET framework applications.
www.OnBarcode.com
Create USS Code 128 In Visual C#.NET
Using Barcode encoder for Visual Studio .NET Control to generate, create ANSI/AIM Code 128 image in VS .NET applications.
www.OnBarcode.com
6. In the Main method, write code to create a new instance of a DirectoryInfo object for the Windows directory and use it to call the new ShowDirectory method. For example, the following code would work:
Generating QR Code 2d Barcode In C#
Using Barcode maker for VS .NET Control to generate, create QR Code JIS X 0510 image in .NET framework applications.
www.OnBarcode.com
Code 3/9 Drawer In Visual C#
Using Barcode creation for .NET Control to generate, create Code39 image in Visual Studio .NET applications.
www.OnBarcode.com
' VB Dim dir As DirectoryInfo = New DirectoryInfo(Environment.SystemDirectory) ShowDirectory(dir) // C# DirectoryInfo dir = new DirectoryInfo(Environment.SystemDirectory); ShowDirectory(dir);
Barcode Printer In C#
Using Barcode printer for .NET framework Control to generate, create barcode image in VS .NET applications.
www.OnBarcode.com
USPS POSTNET Barcode Encoder In C#.NET
Using Barcode creator for .NET framework Control to generate, create Postnet 3 of 5 image in VS .NET applications.
www.OnBarcode.com
7. Build the project and resolve any errors. Verify that the console application successfully lists all the files in the system directory (Environment.SystemDirectory).
EAN-13 Scanner In C#
Using Barcode decoder for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Bar Code Reader In Visual Studio .NET
Using Barcode Control SDK for ASP.NET Control to generate, create, read, scan barcode image in ASP.NET applications.
www.OnBarcode.com
Exercise 2: Watch for Changes in the File System
Code 128 Code Set A Encoder In .NET Framework
Using Barcode maker for .NET framework Control to generate, create Code 128 image in .NET applications.
www.OnBarcode.com
Bar Code Generator In .NET
Using Barcode creator for ASP.NET Control to generate, create barcode image in ASP.NET applications.
www.OnBarcode.com
In this exercise, you will watch the file system for changes in all files that end with the .ini extension. 1. Create a new console application named FileWatchingDemo. 2. Import the System.IO namespace into the new file. 3. Create a new instance of the FileSystemWatcher class, specifying the system directory. For example, you could use the following code:
Encoding UPCA In Objective-C
Using Barcode generation for iPad Control to generate, create UPC-A image in iPad applications.
www.OnBarcode.com
Recognizing UPCA In VB.NET
Using Barcode scanner for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
' VB Dim watcher As New FileSystemWatcher(Environment.SystemDirectory) // C# FileSystemWatcher watcher = new FileSystemWatcher(Environment.SystemDirectory);
UCC.EAN - 128 Generator In Java
Using Barcode encoder for BIRT Control to generate, create GS1 128 image in Eclipse BIRT applications.
www.OnBarcode.com
Barcode Generation In Java
Using Barcode generation for Java Control to generate, create bar code image in Java applications.
www.OnBarcode.com
Lesson 1: Navigating the File System
4. Modify properties of the file system watcher to look only for .ini files, search through all subdirectories, and accept changes only if the attributes of the file change or if the file size changes. Your code might look like this:
' VB watcher.Filter = "*.ini" watcher.IncludeSubdirectories = True watcher.NotifyFilter = _ NotifyFilters.Attributes Or NotifyFilters.Size // C# watcher.Filter = "*.ini"; watcher.IncludeSubdirectories = true; watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.Size;
5. To see the changes, add a handler for the Changed event of your watcher object. For example, you could use the following code:
' VB AddHandler watcher.Changed, _ New FileSystemEventHandler(AddressOf watcher_Changed) // C# watcher.Changed += new FileSystemEventHandler(watcher_Changed);
6. Next you need the method that the Changed event is going to call. Inside this method, write out to the console the name of the changed file. Your code might look something like this:
' VB Sub watcher_Changed(ByVal sender As Object, _ ByVal e As FileSystemEventArgs) Console.WriteLine("Changed: {0}", e.FullPath) End Sub // C# static void watcher_Changed(object sender, FileSystemEventArgs e) { Console.WriteLine("Changed: {0}", e.FullPath); }
7. Set the EnablingRaisingEvents property to true to tell the watcher object to start throwing events.
2
Input/Output (I/O)
8. Build the project and resolve any errors. Verify that the console application successfully reports when the attributes of any .ini file change or when the file size changes.
Lesson Summary
The FileInfo, DirectoryInfo, and DriveInfo classes can be used to enumerate and inspect the properties of file system objects. The Path class can be used to interrogate a file system path and should be used instead of parsing the string manually. The FileSystemWatcher class can be used to monitor the file system for changes such as additions, deletions, and renamings.
Lesson Review
You can use the following questions to test your knowledge of the information in Lesson 1, Navigating the File System. The questions are also available on the companion CD if you prefer to review them in electronic form.
NOTE Answers
Answers to these questions and explanations of why each answer choice is right or wrong are located in the Answers section at the end of the book.
1. Which are acceptable ways to open a file for writing (Choose all that apply.) A.
' VB File.Open("somefile.txt", FileMode.Create) // C# File.Open("somefile.txt", FileMode.Create);
' VB File.Open("somefile.txt", FileMode.Create, FileAccess.Write) // C# File.Open("somefile.txt", FileMode.Create, FileAccess.Write);
' VB File.Open("somefile.txt", FileMode.Create, FileAccess.Read) // C# File.Open("somefile.txt", FileMode.Create, FileAccess.Read);
Lesson 1: Navigating the File System
' VB Dim file As new FileInfo("somefile.txt") file.Open(FileMode.Create) // C# FileInfo file = new FileInfo("somefile.txt"); file.Open(FileMode.Create);
2. Which of the following are types of changes that can be detected by the FileSystemWatcher (Choose all that apply.) A. New files B. New directories C. Changed files D. Renamed files E. None 3. The following code changes the extension of a file. (True or False)
' VB Dim ourPath As String = "c:\boot.ini" Path.ChangeExtension(ourPath, "bak") // C# string ourPath = @"c:\boot.ini"; Path.ChangeExtension(ourPath, "bak");
Copyright © OnBarcode.com . All rights reserved.