Webclient.Downloadfile to get the latest version

  • Thread starter Thread starter James Write
  • Start date Start date
J

James Write

Hi,

I am using the webclient.downloadfile() method to download a file from
a remote web server to the local harddrive.

public CopyFile(string sSrcPath, string sDestPath)
{
WebClient oClient = new WebClient();
oClient.DownloadFile(sSrcPath,sDestPath);
}

Is there anyway i can determine that if the local file exists already
and the file from the web server is newer download it, otherwise
leave it? (Conditional Get) tried accessing the Headers property and
setting the "if-modified-since" to that of the localfiles last write
time, but i get an exception that i can't modify that header property.

try
{
FileInfo oFileInfo = new FileInfo(sDestPath);
if(oFileInfo.Exists)
{
DateTime t = oFileInfo.LastWriteTime.ToUniversalTime();
oClient.Headers["if-modified-since"] = t.ToString("r");
}

oClient.DownloadFile(sSrcPath,sDestPath);
}


Any ideas greatly appreciated.
Cheers.
 
James said:
Hi,

I am using the webclient.downloadfile() method to download a file from
a remote web server to the local harddrive.

public CopyFile(string sSrcPath, string sDestPath)
{
WebClient oClient = new WebClient();
oClient.DownloadFile(sSrcPath,sDestPath);
}

Is there anyway i can determine that if the local file exists already
and the file from the web server is newer download it, otherwise
leave it? (Conditional Get) tried accessing the Headers property and
setting the "if-modified-since" to that of the localfiles last write
time, but i get an exception that i can't modify that header property.

There are a couple of HTTP headers that cannot be set using the Headers
property. These headers are either set implicitly (e.g. Host) or exposed as
individual properties (e.g. IfModifiedSince) of the HttpWebRequest class.
Thus, you'll have to use the lower level classes HttpWebRequest and
HttpWebResponse to make use of these HTTP features.

Cheers,
 
Back
Top