-1

I am new to C# and I have an SSIS script task that I want want to move a file on a ftp to another folder but I want to make sure I close the connection after if it is open still.

        //Move the File to Archive Folder
        FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(FTPFileFullPath);
        ftpRequest.Credentials = new NetworkCredential(UserName, Password);
        ftpRequest.Method = WebRequestMethods.Ftp.Rename;
        ftpRequest.RenameTo = ArchFilePath;
        FtpWebResponse ftpResp = (FtpWebResponse)ftpRequest.GetResponse();

        ftpRequest.KeepAlive = false; // close connection... not sure
        Dts.TaskResult = (int)ScriptResults.Success;

I have put .KeepAlive = false, is this the correct method, any tips most welcome.

Roger Clerkwell
  • 406
  • 4
  • 19
  • 3
    Check the FtpWebRequest and [FtpWebResponse](https://learn.microsoft.com/en-us/dotnet/api/system.net.ftpwebresponse?view=netframework-4.7.2) documentation. The `KeepAlive` property does not do what you think – Cleptus May 17 '21 at 12:39
  • Have you seen this [related question](https://stackoverflow.com/questions/24050381/how-to-properly-disconnect-from-ftp-server-with-ftpwebrequest)? – Axel Kemper May 17 '21 at 12:40
  • So if I have understood correctly from the link ftpRequest.KeepAlive = false; // this drops the object not the connection? ftpWebResponse.Close(); // this closes the connection? – Roger Clerkwell May 17 '21 at 13:14

1 Answers1

2

FtpWebResponse implements IDisposable so you should place it in a using block to ensure the streams are closed.

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
  • Also setting the `FtpWebRequest.KeepAlive` property to false before calling the `GetResponse()` could be useful – Cleptus May 17 '21 at 12:41