- Install the edtFTPnet/Express trial or the edtFTPnet/PRO trial.
- Create a new Windows Forms application in Visual Studio or Visual C#.
- Drag an ExFTPConnection, a FTPStatusBar and two buttons onto the form. (Note: If you are using Visual C# then you'll need to add the controls to the toolbox first - see here).
- Change the names of the buttons to "upload" and "cancel".
- Add Click event-handlers for both buttons (you can do this by double-clicking in each of them in the Designer)
- Replace the code in Form1.cs with the code shown below.
- Change the strings in the constructor to match your server (i.e. "your-server-address", "your-username", etc)
- Run it.
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private string localDir;
private string fileName;
public Form1()
{
InitializeComponent();
// set up connection
exFTPConnection1.ServerAddress = "your-server-address";
exFTPConnection1.UserName = "your-username";
exFTPConnection1.Password = "your-password";
exFTPConnection1.ServerDirectory = "your-server-directory";
localDir = "your-local-directory";
fileName = "your-file-name";
}
private void upload_Click(object sender, EventArgs e)
{
upload.Enabled = false;
cancel.Enabled = true;
// start connecting.
// OnConnected will be called when connection attempt completes (whether successfully or not)
exFTPConnection1.BeginConnect(new AsyncCallback(OnConnected), null);
}
private void OnConnected(IAsyncResult res)
{
try
{
// EndConnect will report any errors that occurred while connecting
exFTPConnection1.EndConnect(res);
// start uploading file.
// OnUploaded will be called when upload attempt completes (whether successfully or not)
string localPath = System.IO.Path.Combine(localDir, fileName);
exFTPConnection1.BeginUploadFile(localPath, fileName, new AsyncCallback(OnUploaded), null);
}
catch (Exception ex)
{
upload.Enabled = true;
cancel.Enabled = false;
MessageBox.Show(ex.ToString());
}
}
private void OnUploaded(IAsyncResult res)
{
try
{
// EndConnect will report any errors that occurred while connecting
exFTPConnection1.EndUploadFile(res);
MessageBox.Show("Upload complete");
}
catch (EnterpriseDT.Net.Ftp.FTPTransferCancelledException)
{
MessageBox.Show("Upload aborted by user");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
// start closing connection
// OnClosed will be called when closed
exFTPConnection1.BeginClose(new AsyncCallback(OnClosed), null);
}
private void OnClosed(IAsyncResult res)
{
// we don't call exFTPConnection1.EndClose(res) since don't really care
// if a problem occurred while closing since the connection
// will always be closed anyway
upload.Enabled = true;
cancel.Enabled = false;
}
private void cancel_Click(object sender, EventArgs e)
{
exFTPConnection1.CancelTransfer();
}
}
}
- Hans (EnterpriseDT)