简体   繁体   中英

Check if a remote file exists

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!

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

顺便说一句,如果您只需要检查文件是否存在而无需实际下载,则应该使用HEAD请求(HttpWebRequest.Method =“ HEAD”)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM