- Home /
Check if file exists on Internet
Hi,
I've been trying to find a way to check if a file exists on the internet using Unity Java. All references I find is about checking for a local file. For example:
if (System.IO.File.Exists("myfile.txt"))
{
print("File Found!");
}
From what I can make out, the above method is not compatible with WWW. Does anyone have any suggestions?
Thanks
Paul
Answer by DaveA · Aug 29, 2012 at 11:42 PM
Check the error returned by the WWW, or use a WWW replacement (find in the Asset Store)
Hi Dave, thanks for the reply. I am able to download via WWW but actually looking for a way to simply check if the file exists as it is for a menu choice and not to be downloaded straight away.
So there have been no other suggestions, so I will go with your suggestion as being the closest there is to an answer. Thanks Dave!
Answer by cregox · Nov 06, 2012 at 04:09 PM
Me likes something like this:
static public bool WebFileExists (string uri) {
long fileLength = -1;
WebRequest request = HttpWebRequest.Create(uri);
request.Method = "HEAD";
WebResponse resp = null;
try {
resp = request.GetResponse();
} catch {
resp = null;
}
if (resp != null) {
long.TryParse(resp.Headers.Get("Content-Length"), out fileLength);
}
return fileLength > 0;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613080355im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Get txt out of web? 1 Answer
Debug Statement Doesn't Work? 2 Answers
How to simply check if a file exists on the hard drive? 1 Answer