Reputation: 595
I need to check if a file or no. of files having a pattern in their file names are available on HTTP. and IF present then next will be to download them.
I know to check a particular file directly on HTTP but I am not sure how to achieve this with File name pattern like abc*.csv?
Upvotes: 0
Views: 283
Reputation: 7973
First of all you must check if the page exist:
using System.Net;
...
private bool CheckIfRemoteFileExist(string url){
try
{
//Creating the HttpWebRequest
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
//Setting the Request method HEAD, you can also use GET too.
request.Method = "HEAD";
//Getting the Web Response.
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//Returns TURE if the Status code == 200
return (response.StatusCode == HttpStatusCode.OK);
}
catch
{
//Any exception will returns false.
return false;
}
}
Then, if it exist, you can download the page:
string content=string.Empty;
if(CheckIfRemoteFileExist("myUrl"))
using(var client = new WebClient())
content = client.DownloadString("myUrl");
else
MessageBox.Show("File seems dosen't exist");
Upvotes: 1