Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Simple FTP Upload

Download Chilkat .NET for 2.0 / 3.5 Framework

Download Chilkat .NET for 1.0 / 1.1 Framework

Simple example to upload a file to an FTP server.

Chilkat.Ftp2 ftp = new Chilkat.Ftp2();

bool success;

// Any string unlocks the component for the 1st 30-days.


success = ftp.UnlockComponent("Anything for 30-day trial");
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

ftp.Hostname = "ftp.chilkatsoft.com";
ftp.Username = "****";
ftp.Password = "****";

// The default data transfer mode is "Active" as opposed to "Passive".

// Connect and login to the FTP server.


success = ftp.Connect();
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

// Change to the remote directory where the file will be uploaded.


success = ftp.ChangeRemoteDir("junk");
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

// Upload a file.
string localFilename;
localFilename = "hamlet.xml";
string remoteFilename;
remoteFilename = "hamlet.xml";

success = ftp.PutFile(localFilename,remoteFilename);
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

ftp.Disconnect();

MessageBox.Show("File Uploaded!");
Passive FTP Upload
Download Chilkat .NET for 2.0 / 3.5 Framework

Download Chilkat .NET for 1.0 / 1.1 Framework

Simple example to upload a file to an FTP server using passive mode.

Chilkat.Ftp2 ftp = new Chilkat.Ftp2();

bool success;

// Any string unlocks the component for the 1st 30-days.


success = ftp.UnlockComponent("Anything for 30-day trial");
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

ftp.Hostname = "www.example-code.com";
ftp.Username = "****";
ftp.Password = "****";

// The default data transfer mode is "Active" as opposed to "Passive".


// Change it to Passive by setting the Passive property:
ftp.Passive = true;

// Connect and login to the FTP server.


success = ftp.Connect();
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

// Change to the remote directory where the file will be uploaded.


success = ftp.ChangeRemoteDir("junk");
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

// Upload a file.
string localFilename;
localFilename = "hamlet.xml";
string remoteFilename;
remoteFilename = "hamlet.xml";

success = ftp.PutFile(localFilename,remoteFilename);
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

ftp.Disconnect();
MessageBox.Show("File Uploaded!");

Simple FTP Download


Download Chilkat .NET for 2.0 / 3.5 Framework

Download Chilkat .NET for 1.0 / 1.1 Framework

Simple example to download a file from an FTP server.

Chilkat.Ftp2 ftp = new Chilkat.Ftp2();

bool success;

// Any string unlocks the component for the 1st 30-days.


success = ftp.UnlockComponent("Anything for 30-day trial");
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

ftp.Hostname = "www.example-code.com";
ftp.Username = "****";
ftp.Password = "****";

// The default data transfer mode is "Active" as opposed to "Passive".

// Connect and login to the FTP server.


success = ftp.Connect();
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

// Change to the remote directory where the file is located.


// This step is only necessary if the file is not in the root directory
// for the FTP account.
success = ftp.ChangeRemoteDir("junk");
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

// Download a file.
string localFilename;
localFilename = "hamlet.xml";
string remoteFilename;
remoteFilename = "hamlet.xml";

success = ftp.GetFile(remoteFilename,localFilename);
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

ftp.Disconnect();

MessageBox.Show("File Downloaded!");

Passive FTP Download


Download Chilkat .NET for 2.0 / 3.5 Framework

Download Chilkat .NET for 1.0 / 1.1 Framework

Simple example to download a file from an FTP server using passive mode.

Chilkat.Ftp2 ftp = new Chilkat.Ftp2();

bool success;

// Any string unlocks the component for the 1st 30-days.


success = ftp.UnlockComponent("Anything for 30-day trial");
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

ftp.Hostname = "www.example-code.com";
ftp.Username = "****";
ftp.Password = "****";

// The default data transfer mode is "Active" as opposed to "Passive".


// Set the Passive property to use Passive mode FTP transfers.
ftp.Passive = true;

// Connect and login to the FTP server.


success = ftp.Connect();
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

// Change to the remote directory where the file is located.


// This step is only necessary if the file is not in the root directory
// for the FTP account.
success = ftp.ChangeRemoteDir("junk");
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

// Download a file.
string localFilename;
localFilename = "hamlet.xml";
string remoteFilename;
remoteFilename = "hamlet.xml";

success = ftp.GetFile(remoteFilename,localFilename);
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

ftp.Disconnect();

MessageBox.Show("File Downloaded!");

Rename Remote File


Download Chilkat .NET for 2.0 / 3.5 Framework

Download Chilkat .NET for 1.0 / 1.1 Framework

Rename a remote file or directory. The RenameRemoteFile can be called to rename either a
remote file or directory.

Chilkat.Ftp2 ftp = new Chilkat.Ftp2();

bool success;

// Any string unlocks the component for the 1st 30-days.


success = ftp.UnlockComponent("Anything for 30-day trial");
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

ftp.Hostname = "ftp.myFtpServer123.com";
ftp.Username = "***";
ftp.Password = "***";

// Connect and login to the FTP server.


success = ftp.Connect();
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

// Set the current remote directory to where the file


// is located:
success = ftp.ChangeRemoteDir("/testing");
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}
// Rename the remote file (or directory)
string existingFilename;
existingFilename = "hello.txt";
string newFilename;
newFilename = "goodbye.txt";
success = ftp.RenameRemoteFile(existingFilename,newFilename);
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

ftp.Disconnect();

Delete
Chilkat.Ftp2 ftp = new Chilkat.Ftp2();

bool success;

// Any string unlocks the component for the 1st 30-days.


success = ftp.UnlockComponent("Anything for 30-day trial");
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

ftp.Hostname = "ftp.chilkatsoft.com.com";
ftp.Username = "myLogin";
ftp.Password = "myPassword";

// Connect and login to the FTP server.


success = ftp.Connect();
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

// Set the current remote directory to where the file


// is located:
success = ftp.ChangeRemoteDir("/testing");
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

success = ftp.DeleteRemoteFile("goodbye.txt");
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

ftp.Disconnect();
File Existence Check
Download Chilkat .NET for 2.0 / 3.5 Framework

Download Chilkat .NET for 1.0 / 1.1 Framework

Testing to see if a file exists on the FTP server. The GetSizeByName method is a convenient
way to check if a file exists. It will return -1 if the file does not exist, otherwise it returns the size
of the file in bytes.

Chilkat.Ftp2 ftp = new Chilkat.Ftp2();

bool success;

// Any string unlocks the component for the 1st 30-days.


success = ftp.UnlockComponent("Anything for 30-day trial");
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

ftp.Hostname = "ftp.***.com";
ftp.Username = "***";
ftp.Password = "***";

// Connect and login to the FTP server.


success = ftp.Connect();
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

// Set the current remote directory to where the file is located:


success = ftp.ChangeRemoteDir("/something");
if (success != true) {
MessageBox.Show(ftp.LastErrorText);
return;
}

// Test to see if the file exists by getting the file size by name.
// If a -1 is returned, the file does not exist.
int fileSize;
fileSize = ftp.GetSizeByName("test123.txt");
if (fileSize < 0) {
textBox1.Text += "file does not exist" + "\r\n";
}
else {
textBox1.Text += "file exists and is " + Convert.ToString(fileSize)
+ " bytes in size" + "\r\n";
}

ftp.Disconnect();
FTP Upload with Progress Monitoring
Download Chilkat .NET for 2.0 / 3.5 Framework

Download Chilkat .NET for 1.0 / 1.1 Framework

Demonstrates how to upload a file to an FTP server with progress monitoring event
callbacks. Also demonstrates how to abort an FTP upload while in progress.

// Progress monitor callback -- called each time the percentage


completion
// updates to a larger value.
public void OnFtpPercentDone(object source,
Chilkat.FtpPercentDoneEventArgs args)
{
// args.PercentDone is an integer value between 1 and 100.
progressBar1.Value = args.PercentDone;

// args.Abort can be set to True to abort the FTP while in


progress.
// This example aborts the FTP transfer after it is 50%
complete.
if (args.PercentDone >= 50)
{
args.Abort = true;
}
}

// FTP Put with progress monitoring event callbacks.


// Upload a file to an FTP server with percent done callbacks.
private void button5_Click(object sender, System.EventArgs e)
{
Chilkat.Ftp2 ftp = new Chilkat.Ftp2();
ftp.UnlockComponent("Any string unlocks for 30-day trial.");

ftp.Hostname = "ftp.chilkatsoft.com";
ftp.Username = "***";
ftp.Password = "***";

ftp.EnableEvents = true;

ftp.OnFtpPercentDone += new
Ftp2.FtpPercentDoneEventHandler(OnFtpPercentDone);

bool success = ftp.Connect();


if (success)
{
success = ftp.PutFile("uploadTest.txt","uploadTest.txt");
if (success)
{
MessageBox.Show("Done!");
}
else
{
MessageBox.Show(ftp.LastErrorText);
}
}
else
{
MessageBox.Show(ftp.LastErrorText);
}
}

FTP Download with Progress Monitoring


Download Chilkat .NET for 2.0 / 3.5 Framework

Download Chilkat .NET for 1.0 / 1.1 Framework

Demonstrates how to download a file from an FTP server with progress monitoring event
callbacks. Also demonstrates how to abort an FTP download while in progress.

// Progress monitor callback -- called each time the percentage


completion
// updates to a larger value.
public void OnFtpPercentDone(object source,
Chilkat.FtpPercentDoneEventArgs args)
{
// args.PercentDone is an integer value between 1 and 100.
progressBar1.Value = args.PercentDone;

// args.Abort can be set to True to abort the FTP trabsfer


while in progress.
// This example aborts the FTP transfer after it is 60%
complete.
if (args.PercentDone >= 60)
{
args.Abort = true;
}
}

// FTP Get with progress monitoring


// Demonstrates how to download a file from an FTP server with
percentage completion event callbacks.
private void button6_Click(object sender, System.EventArgs e)
{
Chilkat.Ftp2 ftp = new Chilkat.Ftp2();
ftp.UnlockComponent("Any string works for 30-day trial");

ftp.Hostname = "ftp.chilkatsoft.com";
ftp.Username = "****";
ftp.Password = "****";

ftp.EnableEvents = true;

ftp.OnFtpPercentDone += new
Ftp2.FtpPercentDoneEventHandler(OnFtpPercentDone);
// Connect and login.
bool success = ftp.Connect();
if (success)
{
// Download a file
success =
ftp.GetFile("ChilkatZipSE.exe","ChilkatZipSE.exe");
if (success)
{
MessageBox.Show("Done!");
}
else
{
MessageBox.Show(ftp.LastErrorText);
}
}
else
{
MessageBox.Show(ftp.LastErrorText);
}
}

You might also like