下面是如何使用ftp上傳文件的代碼。我的問題是如果在try中有一個異常會發生什麼,ftp連接會自動關閉在catch中?使用「使用」更好嗎?ftp連接和上傳如果有異常,連接會發生什麼
謝謝
Try
'connect to ftp server
Dim ftp As New FTPConnection
ftp.ServerAddress = "ftp.example.com"
ftp.UserName = "example_user"
ftp.Password = "example_pass"
ftp.Connect()
ftp.TransferType = FTPTransferType.BINARY
'upload a file
ftp.UploadFile("s:\test.txt", "test.txt")
'close the connection
ftp.Close()
Catch ex As Exception
MessageBox.Show(ex.Message.ToString())
End Try
謝謝, - 如果它在沒有關閉的情況下寫入using語句會發生什麼 - 當發生異常時它會自動關閉?你能幫我寫嗎? –
'使用'編譯爲try/catch/finally,所以'Dispose'將總是被調用 - 您需要確保'Close'方法是作爲'Dispose'執行計劃的一部分調用的通常是可以「打開」的東西)。 –