locked
checking for an existing file RRS feed

Answers

  • User-451260051 posted

    This would also work: 

    using System.Net;

           WebResponse response = null;
            string URL = "www.cnn.com";
            try
            {
                WebRequest request = WebRequest.Create(URL);
                response = request.GetResponse();

                //Perform your work here, if its a valid URL no exception is thrown and the try block executes

                response.Close();

            }
            catch
            {
                 //This will execute if the URL is not valid, it throws an exception
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 7, 2008 4:31 PM

All replies

  • User-99544339 posted
    Hi rkeslar File.Exists can only be used on filesystem files or files on a network path, see the msdn reference http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx

    Maybe you can do it with a WebRequest and check for a 404 (then the file doesn't exists).

    Good luck.
    Tuesday, October 7, 2008 4:03 PM
  • User533502567 posted

    Not sure, why it is not working. try following code, i used it in a project..  

     FileInfo MyFile = new FileInfo("http://intranet.myCompany.com/Employee%20Photos/" + strOfficeId + "/" + strUsername + ".jpg); 
        if (MyFile.Exists()) { 
        } 
        //do something 
        else { 
            //do something 
        } 
    
      
    Tuesday, October 7, 2008 4:13 PM
  • User-451260051 posted

    This would also work: 

    using System.Net;

           WebResponse response = null;
            string URL = "www.cnn.com";
            try
            {
                WebRequest request = WebRequest.Create(URL);
                response = request.GetResponse();

                //Perform your work here, if its a valid URL no exception is thrown and the try block executes

                response.Close();

            }
            catch
            {
                 //This will execute if the URL is not valid, it throws an exception
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 7, 2008 4:31 PM
  • User1057623887 posted

    Here's my problem:

    response = request.GetResponse();   returns null because of this error:

    -  base {"The remote server returned an error: (401) Unauthorized."} System.InvalidOperationException {System.Net.WebException}

    Is there anything I can do about this?

    Thanks

     

    I think I see the problem. Network Service probably needs read access in this folder. I'm not sure if it has it or not.

    Tuesday, October 7, 2008 4:53 PM