if file exist on the net

F

Franck

i have already a way to know if a file is there by using
System.Net.WebRequest.Create(strURL);

the problem come when checking for huge file. How do i manage that.
because the code define over there read the entire file and output a
string in my case. Is there any other system.net object that could
check only if a file exist.
 
N

Nicholas Paldino [.NET/C# MVP]

Franck,

Do you mean on a web server (through the HTTP or HTTPS protocol)? If
so, you can issue a HEAD statement and check the response code (it will be
200 if it exists, 404 if not found). Then, if the item exists, you can
issue a GET (or POST, if that is what you are using) to get the actual
content.
 
F

Franck

sorry i forgot to explain all. Im using C# windows app and the purpose
is for update application. We already have a webservice on the server
and we use the same server on http port to get the file. i download it
without problem it's just that now i need to add a check if the file
exist.

separate Exe which use System.Net.WebRequest.Create(strURL); on an
xml file on the server that contain the current version. if empty
result mean the user is offline. if it return something i compare the
versions and if higher i update the program.
 
N

Nicholas Paldino [.NET/C# MVP]

Franck,

Not to get completely off topic, but since .NET 2.0 (and even before,
with the updater application block in the enterprise library), you haven't
had to do this for yourself because of ClickOnce.

You might want to look into that, as I imagine it will make your life
much, much easier.
 
F

Franck

I tested it and it work on a test project, but our project have too
much dependencies to use that installer. According to the list of
feature i found, the apllication is only install for one user and
inside what's called "clickonce application cache." we actually need
it to be install on multiple user. We aslo have old unmanage dll that
we need to register when first installed. Unless there is special
extra feature somewhere we will keep the basic installer and update im
working on.

So i ask the question again.

Is there a way to know if a file exist using URL ?
 
N

Nicholas Paldino [.NET/C# MVP]

Franck,

The method I gave you before will work for HTTP/HTTPS urls, where you
issue a HEAD request to get just the headers. If you get a 200 response,
then you know the item is there. If you get a 404 response, then you know
it doesn't exist.
 
A

Arne Vajhøj

Franck said:
i have already a way to know if a file is there by using
System.Net.WebRequest.Create(strURL);

the problem come when checking for huge file. How do i manage that.
because the code define over there read the entire file and output a
string in my case. Is there any other system.net object that could
check only if a file exist.

See code below for inspiration.

Arne

===============================================

public enum WebTestResult
{
STATUS_EXIST,
STATUS_NOT_EXIST,
STATUS_NO_HOST,
STATUS_NOT_HTTP
}
public class WebTest
{
public static WebTestResult Exist(string url)
{
try
{
WebRequest wreq = WebRequest.Create(url);
wreq.Method = "HEAD";
WebResponse wresp = wreq.GetResponse();
if(wresp is HttpWebResponse)
{
HttpWebResponse hwresp = (HttpWebResponse)wresp;
if(hwresp.StatusCode == HttpStatusCode.OK)
{
return WebTestResult.STATUS_EXIST;
}
else
{
return WebTestResult.STATUS_NOT_EXIST;
}
}
else
{
return WebTestResult.STATUS_NOT_HTTP;
}
}
catch(WebException wex)
{
if(wex.Response == null)
{
return WebTestResult.STATUS_NO_HOST;
}
else
{
if(wex.Response is HttpWebResponse)
{
HttpWebResponse hwresp =
(HttpWebResponse)wex.Response;
if(hwresp.StatusCode == HttpStatusCode.OK)
{
throw new SystemException("Should never happen");
}
else
{
return WebTestResult.STATUS_NOT_EXIST;
}
}
else
{
return WebTestResult.STATUS_NOT_HTTP;
}
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top