File Download Utility
Download Source Code – srvDownload.zip
System Requirement:
Windows2000
Microsoft .NET Framework Beta 1
Internet Information Server (IIS 5.0)
Visual Studio 7.0 Beta 1
Programming Language:C#
Purpose:
The purpose of this utility is to understand the following using .NET Framework:
-How to write and install Window Services,
-How to write internet Application using Http protocol.
-How to use Streams to read and write in a file.
Description:
File download utility is a web application which resides on the webserver and monitors a txt file in c:\temp subdirectory. If it detects achange in this file, it downloads compPrj.dll from the web server("http://localhost/files/compPrj.dll") to C:\ root directory. I havehardcoded file names and paths to simplify the program.
This Application is installed as Windows2000 Service and it uses FileSystemWatcher component for monitoring the files.
NOTE: FileSystemWatcher needs .NET framework.
The Application consist of two programs srvDownload.cs and srvInstaller.cs.
SrvDownload:
-SrvDownload is Windows2000 service class. From the VS IDE selectwindows Service project. A template class will be created with theconstructor and all the required methods.
-In the InitializeComponent method assign the service a name( like srvDownload).
-In the Onstart method create an instance of FileWatcher component and set the following properties:
| .Path | The path of the file that is to be monitored. |
| .Target | Indicates whether files, directory or both needs monitoring. |
| .ChangedFilters | Specifies the changes to watch for in a file or folder. The possible values are: .Attributes – The attributes of the file or folder. .LastAccess – The date that the file or folder was last opened. .LastWrite – The date that the file or folder was last written. .Security – The security settings of the file or folder. .Size – The size of the file or folder. |
| .Enabled | Indicates whether the component is enabled. |
| .Filter | Indicates what files are monitored in a directory. For all files use *.*, for Text use txt. |
-Finally, add the Event handler. Since, this Application is onlymonitoring the CHANGED event therefore only onChanged event is added.However, you can add other events that you want to monitor such asonDelete or on Rename etc.
When a file is changed, the OnChange event is fired which downloads file CompPrj.dll from the web server to the client machine's
c:\ direcory. The download functionality uses .NET classesHttpWebResponse, HttpWebRequest. HttpWebRequest class manages thedetails of connecting to an Internet resource via Http. WebResponseclass acts as a container for the incoming response. This response isthen stored in a file using .NET I/O stream classes.
SrvInstaller.cs
An installer is needed to install the service therefore add installerto the same project solution. A template class will be created with theconstructor.
In order to install services, ServiceInstallerand ServiceProcessInstaller classes from System.ServiceProcessnamespace is required, one for the service and another for serviceprocess respectively.
Therefore instances of these classes iscreated inside the installer constructor. You can also setServiceInstaller properties in the constructo such as Display Name,Service name etc. After setting the properties, add the service toInstallers collection.
Service Installation:
-Build your project using VS Build.
-Install the service using InstallUtil from the Command prompt.
e.g. c:>installutil drvDownload.exe
-Aftersuccessfully installing the utility, go to MyComputer and selectManage, this will take you to Computer Management screen. SelectServices and you will see the drvDownload service.
-Start the Service.
-Change or Create a txt file in c:\temp subdirectory and this willdownload compprj.dll from the web server virtual directory files to c:\root directory.




25. Nov, 2005 by 







No comments yet... Be the first to leave a reply!