Version: 4.5.0.0
Easily add comprehensive, secure FTP to any .NET application or service.

Windows Forms Get Code Example

The following example demonstrates an FTP session in a Windows Forms environment, in which a file is downloaded from the server. The session is executed asynchronously, without blocking the UI. The session is logged and progress is provided during the transfer.

< Back to all samples for PowerTCP FTP for .NET 

private void butGet_Click(object sender, EventArgs e)
{
    //This application has a User-Interface, which we do not want to block.
    //Start launches the specified method on a worker thread.
    //Event handlers execute on the UI thread.
    ftp1.Start(getFile, null);
}

private void getFile(object state)
{
    //This function performs an FTP Get operation using blocking calls.
    //Use the Start method to execute this function on a worker thread.
    try
    {
        //Set Session variables. Session is a serializable object that can be easily persisted.
        ftp1.Session.RemoteEndPoint.HostNameOrAddress = "myFtpServer";
        ftp1.Session.Username = "myUsername";
        ftp1.Session.Password = "myPassword";

        //Connect and authenticate.
        ftp1.Connect();
        ftp1.Authenticate();

        //Get the file and marshal the result back to the UI thread via the Copy event.
        CopyResult result = ftp1.Get("myFile.pdf""c:\\MyFiles\\myFile.pdf"Synchronize.Off);
        ftp1.Marshal(result, ""null);
    }
    catch (Exception ex)
    {
        //Marshal exceptions to the UI thread via the Error event.
        ftp1.Marshal(ex);
    }
    finally
    {
        //Logout and close the connection.
        ftp1.Close();
    }
}

private void ftp1_Progress(object sender, Dart.Ftp.ProgressEventArgs e)
{
    //Update a progress bar using information from the Progress event.
    progressBar.Value = Convert.ToInt32(e.Progress.Count * 100 / e.Progress.Length);
}

private void ftp1_Connection_Log(object sender, Dart.Ftp.DataEventArgs e)
{
    //Update a log with communication between the application and the server.
    textLog.AppendText(e.Data.ToString());
}

private void ftp1_Copy(object sender, CopyEventArgs e)
{
    //This event fires when a CopyResult is marshaled.
    textLog.AppendText("Operation completed: " + e.Result.Status.ToString() + "\r\n");
}

private void ftp1_Error(object sender, Dart.Ftp.ErrorEventArgs e)
{
    //Update the log with any exceptions that occur.
    textLog.AppendText("ERROR: " + e.GetException().Message + "\r\n");
}