finding if a file exists

  • Thread starter Thread starter Carlos
  • Start date Start date
C

Carlos

Hi all,

I need to find out if a file exists given a local, or remote path. That is,
if it resides the local machine, the network, or in a given URL . Is there
any way to do that?

Thanks in advance,

Carlos.
 
Hi all,

 I need to find out if a file exists given a local, or remote path. That is,
if it resides the local machine, the network, or in a given URL . Is there
any way to do that?

Thanks in advance,

  Carlos.

if (s.StartsWith("http") || s.StartsWith("ftp"))
.... remote path
elseif ( s.StartsWith("\\") )
.... network path
else
.... local
 
Hi again Alexey,

thanks for your prompt response. However, could you also please fill in the
blanks for those conditions. In other words, how would I check for the
existence in the URL path, and network. I know I just can use
system.io.file.exists for local resources.

Thanks again,

Carlos.

Hi all,

I need to find out if a file exists given a local, or remote path. That
is,
if it resides the local machine, the network, or in a given URL . Is there
any way to do that?

Thanks in advance,

Carlos.

if (s.StartsWith("http") || s.StartsWith("ftp"))
.... remote path
elseif ( s.StartsWith("\\") )
.... network path
else
.... local
 
You would do a web request to try the file, and if it returns a 404 error,
it is not there.

It might also be worth checking other responses that 200, for example, a 301
or a 302 is a redirect.

--
Best regards,
Dave Colliver.http://www.AshfieldFOCUS.com
~~http://www.FOCUSPortals.com- Local franchises available











- Show quoted text -

Carlos,

try like Dave suggested

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create
("http://....");
webRequest.AllowAutoRedirect = false;
try
{
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
Response.Write(response.StatusCode.ToString()); // Returns "OK",
"Moved", "MovedPermanently", etc
}
catch (WebException ex)
{
Response.Write(ex.Message); // File does not exist
}

Hope this helps
 
Hi again Alexey,

 thanks for your prompt response. However, could you also please fill in the
blanks for those conditions. In other words, how would I check for the
existence in the URL path, and network. I know I just can use
system.io.file.exists for local resources.

Regarding network check. system.io.file.exists should work there too,
but you have to be aware of access rights from ASP.net process to the
shared resource.

if (System.IO.File.Exists(@"\\Server\Folder\File.txt")) {
....
}
 
Back
Top