C#的文件下载
这是一个运行在web服务器上并且监视c:\temp中的一个文本文件,如果发生了变化
它就自动将compPrj.dll从服务器下载到本地的c:\root目录。这个应用作为Windows 2000的服务安装并且用FileSystemWatcher组件监视文件。
注意: FileSystemWatcher 需要 .NET framework
System Requirement:
Windows2000
Microsoft .NET Framework Beta 1
Internet Information Server (IIS 5.0)
Visual Studio 7.0 Beta 1Programming 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 web server and monitors a txt file in c:\temp subdirectory. If it detects a change in this file, it downloads compPrj.dll from the web server ("http://localhost/files/compPrj.dll") to C:\ root directory. I have hardcoded 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 select windows Service project. A template class will be created with the constructor 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 only monitoring the CHANGED event therefore only onChanged event is added. However, you can add other events that you want to monitor such as onDelete 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 classes HttpWebResponse, HttpWebRequest. HttpWebRequest class manages the details of connecting to an Internet resource via Http. WebResponse class acts as a container for the incoming response. This response is then stored in a file using .NET I/O stream classes.SrvInstaller.cs
An installer is needed to install the service therefore add installer to the same project solution. A template class will be created with the constructor.In order to install services, ServiceInstaller and ServiceProcessInstaller classes from System.ServiceProcess namespace is required, one for the service and another for service process respectively. Therefore instances of these classes is created inside the installer constructor. You can also set ServiceInstaller properties in the constructo such as Display Name, Service name etc. After setting the properties, add the service to Installers collection. Service Installation:
-Build your project using VS Build.
-Install the service using InstallUtil from the Command prompt.
e.g. c:>installutil drvDownload.exe
-After successfully installing the utility, go to MyComputer and select Manage, this will take you to Computer Management screen. Select Services and you will see the drvDownload service. 
-Start the Service.
-Change or Create a txt file in c:\temp subdirectory and this will download compprj.dll from the web server virtual directory files to c:\ root directory.这个网不错 http://www.csharphelp.com