finding if a file exists

Discussion in 'ASP .Net' started by Carlos, Oct 20, 2009.

  1. Carlos

    Carlos 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.
     
    Carlos, Oct 20, 2009
    #1
    1. Advertising

  2. On Oct 20, 4:46 pm, "Carlos" <> wrote:
    > 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
     
    Alexey Smirnov, Oct 20, 2009
    #2
    1. Advertising

  3. Carlos

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

    Thanks again,

    Carlos.

    "Alexey Smirnov" <> wrote in message
    news:...
    On Oct 20, 4:46 pm, "Carlos" <> wrote:
    > 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
     
    Carlos, Oct 20, 2009
    #3
  4. Carlos

    David 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


    "Carlos" <> wrote in message
    news:...
    > 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.
    >
    > "Alexey Smirnov" <> wrote in message
    > news:...
    > On Oct 20, 4:46 pm, "Carlos" <> wrote:
    >> 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
    >
     
    David, Oct 20, 2009
    #4
  5. On Oct 20, 7:18 pm, "David"
    <> wrote:
    > 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
    >
    > "Carlos" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > 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.

    >
    > > "Alexey Smirnov" <> wrote in message
    > >news:....
    > > On Oct 20, 4:46 pm, "Carlos" <> wrote:
    > >> 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- Hide quoted text -

    >
    > - 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
     
    Alexey Smirnov, Oct 20, 2009
    #5
  6. On Oct 20, 6:04 pm, "Carlos" <> wrote:
    > 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")) {
    ....
    }
     
    Alexey Smirnov, Oct 20, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stu
    Replies:
    4
    Views:
    7,347
    Kevin Spencer
    Aug 19, 2003
  2. Stu
    Replies:
    1
    Views:
    370
    Elliot M. Rodriguez, MCSD
    May 20, 2004
  3. Totan
    Replies:
    0
    Views:
    1,084
    Totan
    Apr 17, 2006
  4. Replies:
    2
    Views:
    457
    Eric Sosman
    Feb 5, 2007
  5. Ulf Meinhardt
    Replies:
    8
    Views:
    6,310
Loading...

Share This Page