Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi ,
I have a code ,
C#
WebClient webClient = new WebClient();
webClient.DownloadFileAsync(new Uri("http://MySite.com/Desktop/Pics.png"), @"c:\users\Windows\desktop\DesktopsPics\Desktop.png");


My Program will download a .png picture every day as "Daily Pics" in a folder!
so , when user click on a button, if "Daily Pic" is already exists in server ,program download this file ...
I can do this with above code , but , if Pic.Png is not already exists in server , my program work with error !!!! (it download a .html file that is 404 not found" :)


so how can i download a file , if this file exist in a server ?!? :D

Thanks
Posted
Comments
joshrduncan2012 11-Oct-13 15:48pm    
Your code looks correct. Are you asking how can a file be downloaded if it doesn't already exist on server?
MohammadSina Karvandi 11-Oct-13 15:53pm    
yeah !!!
joshrduncan2012 11-Oct-13 16:09pm    
You can't download something that doesn't exist.
MohammadSina Karvandi 11-Oct-13 16:25pm    
so , can i check (if exit) download it ?
joshrduncan2012 11-Oct-13 16:28pm    
Sure, you can check to see if the file exist and then download it if it does.

1 solution

You can chrck if a file exitst on your web server.

C#
// create the request
HttpWebRequest request = WebRequest.Create(url);;

// instruct the server to return headers only
request.Method = "HEAD";

// make the connection
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

// get the status code
HttpStatusCode status = response.StatusCode;


Now you can check the responce codes against the ones listed here

HTTP_STATUS_NOT_FOUND is what you are looking for.

Source[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900