4

I am building an FTP utility class in C#. In the case that a WebException is thrown on a call to FtpWebRequest.GetResponse(), in my case the exception is thrown for the requested file not existing on the remote server the FtpWebResponse variable is out of scope.

But even if I declare the variable outside the try..catch block I get a compile error saying "Use of unassigned local variable 'response'", but as far as I can tell there is no way to assign it until you assign the response via the FtpWebRequest.GetResponse() method.

Can someone please advise, or am I missing something obvious?

Thanks!

Here is my current method:

private void Download(string ftpServer, string ftpPath, string ftpFileName, string localPath, 
                           string localFileName, string ftpUserID, string ftpPassword)
    {
        FtpWebRequest reqFTP;
        FtpWebResponse response;
        try
        {
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://"
               + ftpServer + "/" + ftpPath + "/" + ftpFileName));
            reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
            reqFTP.UseBinary = true;
            reqFTP.Credentials = new NetworkCredential(ftpUserID,
                                                       ftpPassword);

            /* HERE IS WHERE THE EXCEPTION IS THROWN FOR FILE NOT AVAILABLE*/
            response = (FtpWebResponse)reqFTP.GetResponse();
            Stream ftpStream = response.GetResponseStream();


            FileStream outputStream = new FileStream(localPath + "\\" +
               localFileName, FileMode.Create);

            long cl = response.ContentLength;
            int bufferSize = 2048;
            int readCount;
            byte[] buffer = new byte[bufferSize];

            readCount = ftpStream.Read(buffer, 0, bufferSize);
            while (readCount > 0)
            {
                outputStream.Write(buffer, 0, readCount);
                readCount = ftpStream.Read(buffer, 0, bufferSize);
            }

            ftpStream.Close();
            outputStream.Close();
            response.Close();
        }
        catch (WebException webex)
        {
            /*HERE THE response VARIABLE IS UNASSIGNED*/
            if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable) { 
                //do something
            }
        }
jaywon
  • 8,164
  • 10
  • 39
  • 47

3 Answers3

6

As generic way to solve this, just assign null to the response first and then check in the catch block if it is null.

    FtpWebResponse response = null;
    try
    {
...
    }
    catch (WebException webex)
    {
        if ((response != null) && (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)) { 
            //do something
        }
    }

However, in this specific case, you have all the properties you need on the WebException instance (including the server response)!

Lucero
  • 59,176
  • 9
  • 122
  • 152
  • jeez, i didn't even try that. Thanks for both tips! I looked at the WebExceptionStatus and wasn't sure if there was a type defined specifically for 'file not found'. I will look into that more but the null should do it. – jaywon Mar 11 '10 at 10:38
  • This solves the unassigned local variable problem, but doesn't help catching the exception because 'response' will still be null when the error happens. Check this question for the correct solution: http://stackoverflow.com/questions/347897/how-to-check-if-file-exists-on-ftp-before-ftpwebrequest – Marc Apr 21 '10 at 06:08
  • Marc, note that I was addressing both the problem in general as well as the specific case with the `WebException` - see the bottom paragraph (which is the same solution in short as the one provided in your link). – Lucero Apr 21 '10 at 08:57
  • Ups you're right, sorry I must have missed that part. However when I see code then I prefer reading code and obviously the given code doesn't catch the exception. Maybe you should add a comment to the code itself. – Marc Apr 21 '10 at 14:20
2

The correct solution to the problem can be found in this question here:
How to check if file exists on FTP before FtpWebRequest

In short:
Your 'response' variable will always be null because of the error. You need to test the FtpWebResponse from 'webex.Response' (cast it) to get the StatusCode.

Community
  • 1
  • 1
Marc
  • 9,012
  • 13
  • 57
  • 72
  • You're right, however the code in your answer is misleading as it doesn't help catching the file unavailable exception. – Marc Apr 21 '10 at 14:21
1

Well you could always assign a variable:

FtpWebRequest reqFTP = null;
FtpWebResponse response = null;
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928