0

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
  • 1,190
  • 2
  • 14
  • 29
Chris Lukic
  • 341
  • 1
  • 10
  • 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 B Jun 19 '19 at 18:53

0 Answers0