finding if a file exists

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.
 
G

Guest

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
 
C

Carlos

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
 
G

Guest

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
 
G

Guest

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")) {
....
}
 

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

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top