CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2010
    Posts
    4

    System.Net.WebException was unhandled

    Hey guys I really need some help I have spent hours trying to fiddle this code,

    I have this:-
    Code:
     System.Net.WebClient client= new System.Net.WebClient();
     client.DownloadFile("http://localhost/test.txt", string.Concat("C:/test.txt"));
    ok so that works fine, but I want it so that IF the file did not exist, it would have a message box poping up saying the file could not be found rather than getting a horrible message as such:-

    The remote server returned an error. (404) Not Found.

    I know the file doesnt' exist when I remove it from localhost, but I want to know how to verify whether or not the file exists befpre downloading, or something to give me the ability to handle custom errors!

    Cheers in advance!

  2. #2
    Join Date
    Jun 2010
    Posts
    85

    Re: System.Net.WebException was unhandled

    Code:
    try
    {
         System.Net.WebClient client= new System.Net.WebClient();
         client.DownloadFile("http://localhost/test.txt", string.Concat("C:/test.txt"));
    }
    catch(Exception ex)
    {
        throw new Exception("Some Pretty Message for user", ex) ;
    }
    --------------------------------------------------------------------------------------------------------------------------
    Disclaimer - Most likely any code I have posted as an answer was most likely written free hand and may have some minor compile errors, and is merely intended to give you the idea.

  3. #3
    Join Date
    Jun 2010
    Posts
    4

    Re: System.Net.WebException was unhandled

    Thanks for the quick reply,

    It now says syntax error 'try expected'

  4. #4
    Join Date
    Jun 2010
    Posts
    85

    Re: System.Net.WebException was unhandled

    Is that a compile error that you are getting? Did you make sure to get the "try" at the top?
    --------------------------------------------------------------------------------------------------------------------------
    Disclaimer - Most likely any code I have posted as an answer was most likely written free hand and may have some minor compile errors, and is merely intended to give you the idea.

  5. #5
    Join Date
    Jun 2010
    Posts
    4

    Re: System.Net.WebException was unhandled

    Hi again matt,

    I have this:

    Code:
    try
                    {
                        System.Net.WebClient client = new System.Net.WebClient();
                        client.DownloadFile("http://localhost/test.txt" string.Concat("C:/test.txt"));
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("error", ex);
                    }
    I get this error now:-
    Exception was unhandled in visual c#

  6. #6
    Join Date
    Jun 2010
    Posts
    4

    Re: System.Net.WebException was unhandled

    Just a update I managed to fix it by using the Exception Inner rather than just exception!

    Cheers anyway

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured