Reputation: 10562
When retrieving remote files from a serrver via HTTP, there is one situation where I do not know the exact number of files I fill need to retrieve, incrementing a part of the filename until either the file does not exist or I hit a pre-defined threshold.
What is the best way to test if a remote file exists using C#? Obvisouly I could send a webrequest and see if it times out, but that would not be optimal!
Upvotes: 1
Views: 1164
Reputation: 292695
BTW, if you just need to check whether the file exists, without actually downloading it, you should use a HEAD request (HttpWebRequest.Method = "HEAD")
Upvotes: 5
Reputation:
404 Errors (file not found) don't time out. They return immediately with the status code 404.
Check the response status code of the request. 200 is OK, 404 is File Not Found
Upvotes: 11