Library code snippets
FTP client library for C#
Finding a fully working, lightweight ftp client that had no GUI, was free, and came with source was difficult. This API is based on one Jaimon Mathew had done, and I have added some methods, cleaned up the code, debugged it, added some error handling, logging, debugging etc.
It is easy to use. Here is a code example:
FtpClient ftp = new FtpClient(FtpServer,FtpUserName,FtpPassword);
ftp.Login();
ftp.Upload(@"C:\image.jpg");
ftp.Close();
Not so difficult now is it? I am using it in my WebCamService list on this site.
I started out wrapping fully the WinInet API, but it was sucha labourous task that it made sense to just to the FTP, sinse Microsoft has great support for HTTP, I could skip that, and later work on SMTP.
Features
- Upload
- Recursive Upload
- Download
- Resume
- Delete
- Rename
- Create Directory
- Asynchronous operation
Short-Comings
- Not fully tested
- No real error handling
- No download recurse yet
- Rename will overwrite if the targey already exists
Asynchronous operation
A little more advanced, the asynchronous operation is for when you need the job to fork while your code continues over the method. All asynchronous method start with Begin.
Using it is simple. You need a couple of things, an AsyncCallback object and a method which it handles, like so:
private FtpClient ftp = null;
private void UploadPicture(string imagePath)
{
string FtpServer = ConfigurationSettings.AppSettings["FtpServer"];
string FtpUserName = ConfigurationSettings.AppSettings["FtpUserName"];
string FtpPassword = ConfigurationSettings.AppSettings["FtpPassword"];
AsyncCallback callback = new AsyncCallback(CloseConnection);
ftp = new FtpClient(FtpServer,FtpUserName,FtpPassword);
ftp.Login();
ftp.BeginUpload(imagePath, callback);
ftp.Close();
}
private void CloseConnection(IAsyncResult result)
{
Debug.WriteLine(result.IsCompleted.ToString());
if ( ftp != null ) ftp.Close();
ftp = null;
}
When the upload finishes (or throws an exception), the CloseConnection method will be called.
Related articles
Related discussion
-
Buy cheap Xanax overnight. Cheap Xanax. Overnight delivery of Xanax in US no prescription needed. Cheapest Xanax.
by asleymar (0 replies)
-
Buy Soma online without a prescription. Soma drug no prescription. How to get Soma prescription. Soma cod accepted.
by asleymar (0 replies)
-
Cheap online order Fioricet. Cheap discount Fioricet. Offshore Fioricet online. How to buy Fioricet online without a prescription.
by asleymar (0 replies)
-
Buy Ambien no visa without prescription. Not expensive Ambien prescriptions. Ambien no rx. Cod delivery Ambien.
by asleymar (0 replies)
-
Tramadol without doctor rx. Buy Tramadol over the counter cod overnight. Cheap Tramadol cod delivery. Buy Tramadol from mexico online.
by asleymar (0 replies)
Related podcasts
-
Object-Oriented Programming in Ruby
In this episode, I talk with Scott Bellware about object-oriented programming in Ruby, and Ruby's object model. This is taken from a private conversation, and the audio quality suffers at times. Much thanks to Scott for allowing this to be released.This episode of the Alt.NET Podcast is bro...
http://www.jscape.com/sftpdotnet/
The test is looking at timeout and should be changed to:
while (this.bytes>0)
{
this.bytes = cSocket.Receive(buffer, buffer.Length, 0);
output.Write(this.buffer,0,this.bytes);
bytestransferred += this.bytes;
}
is there anyone here knows how to use the ftp image upload code... instead of it connecting through PASV.... is there anyway i could use PORT instead...
i hope this time someone responds to my questions... because i couldn't figure this out. Please help
Note: Edited by bimboy on 5/2/2005
Can't connect to remote serverSystem.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
Can anyone please help me solve this problem.....
is there anyone here knows how to use the ftp image upload code... instead of it connecting through PASV.... is there anyway i could use PORT instead...
i hope this time someone responds to my questions... because i couldn't figure this out. Please help
i cannot upload a *.gif file on my ftp site... is there anyone who can try and explaine to my why? please and thank you! i can create a folder delete a folder but i can't upload an image file.. any help will be greatly appreciated. thanks again
I have a problem implementing (APPE)ND functionality in this class. One of our clients requested exporting data via FTP, so I need to connect to an FTP and add rows to a textfile. I could just download the file first append new text and upload back to the server, but where's the fun in that?
The method I wrote looks like this and genrates the "Can't open data connection." exception in the createDataSocket() method:
public void AppendToFile(string localFile, string remoteFile)
{
if ( !this.loggedin ) this.Login();
this.sendCommand("APPE " + remoteFile);
if(this.resultCode == 150)
{
Socket socket = this.createDataSocket();
FileStream input = new FileStream(localFile,FileMode.Open);
while ((bytes = input.Read(buffer,0,buffer.Length)) > 0)
{
socket.Send(buffer, bytes, 0);
}
input.Close();
if (socket.Connected)
{
socket.Close();
}
}
}
I can connect, login, upload and download without any issues. In my code I call GetFileList() method before Appending to get the file to append to. But that method cannot hog the socket, right?
Maybe I've misunderstood the usage of the APPE command (even though I've read the entire FTP RFC).
I'm not that experienced in socket programming either, I just copied the code from the Upload method and removed all of the filechecks.
Did I miss something fundamental?
Dan, your program is excellent and I'm delighted to use it in my application. However, it does not transfer files marked as 'read only'. Is there any way I can get around this problem ? Would be delighted if you help me. Thanks.
Raghunath
The BinaryMode property sets the mode of the FTP transmission, but fails to set the binMode variable. This will cause any check of the BinaryMode property to return whatever the initial value of binMode is.
To fix this, just add "this.binMode = value;" after the "if ( this.binMode == value) return;" line.
-Mike
hi..i used your ftp library and everything would have been great but its pretty slow when uploading and downloading stuff. it takes about 5 seconds for a 10k file to be uploaded. i have traced down whats causing it and it seems that it takes about 5 seconds to finish this line....
ep=new IPEndPoint(Dns.Resolve(ipAddress).AddressList[0], port);
this line is found in the createDataSocket routine. is there a workaround for this? ...or is this class just slow or is there another implementation to solve this? thanks
I've posted a program that uploads to FTP & emails at the same time. The FTP functionality is provided by this class, and works okay: thanks!
http://www.wolfsheep.com/map
EDT publishes edtFTPnet, which is a free, open-source FTP client library for .NET. It's published under the LGPL licence meaning developers can incorporate it into their closed-source applications, as long a few simple conditions are met. The URL is http://www.enterprisedt.com/products/edtftpnet/overview.html
I download them I get the 426 Failure Writing Network Stream error.
Does anybody know how to fix this?
This thread is for discussions of FTP client library for C#.