1

I would like to use C# to check if a file exists at a specified url without triggering and catching a 404 error as in the code below.

bool urlExists = true;
try {
    using (var webClient = new WebClient()) {
        byte[] data = webClient.DownloadData(myUrl);                                
    }
}
catch (WebException) {
    urlExists = false;
}

Roberto Pegoraro
  • 1,313
  • 2
  • 16
  • 31
Chris Lukic
  • 353
  • 1
  • 11
  • 3
    Have a look at this: https://stackoverflow.com/questions/14646052/how-to-determine-a-404-response-status-when-using-the-httpclient-getasync – Stefan Jun 19 '19 at 18:43
  • I'd recommend using `HttpClient` over `WebClient` – Chris Jun 19 '19 at 18:53

0 Answers0