المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Scope issue closing a connection after exception



C# Programming
06-28-2012, 04:27 AM
Hello,
I'm having a problem where an exception is thrown while trying to download files, using the Tamir.SharpSSH library, if the files don't exist. It's expected behavior, however, after the exception is thrown I'm unable to close the connection because I lose scope. I'm sure there is a simple solution. I would appreciate any help. The code is currently...

try { Sftp sftp = new Sftp(sftpServer, sftpUserName); sftp.Password = sftpPassword; sftp.OnTransferProgress += new FileTransferEvent(sftp_OnTransferProgress); sftp.OnTransferStart += new FileTransferEvent(sftp_OnTransferStart); sftp.Connect(22); if (sftp.Connected) { DateTime now = DateTime.Now; string year = now.Year.ToString(); string month = now.Month.ToString(); if (month.Length < 2) { month = "0" + month; } Log("Attempting to download all files from todalton directory for the current month"); sftp.Get("balance//todalton//daltonAutoload" + year + month + "*", @"C:\higherone\");//sftp.Close(); Log("Transfer Complete"); } } catch (Tamir.SharpSsh.jsch.SftpException) { Log("No files of the given criteria exist on the server.\r\n\t\tDownload aborted." + "\r\n\t :NOTE: If this is the first run of the month, this is the expected behavior."); } catch (Exception ex) { Log(ex.Message + " :NOTE: sftp connection failed during download"); } finally { sftp.Close(); }
I can't reach the Close method from within finally or either catch. Originally the close was where it is commented. Obviously the exception causes that line to be missed though.

Thanks