共用方式為


Simple FTP Upload in C# w Free FTP DLL

Two nights ago I created an app that lets me just right-click a file in Windows Explorer and it uploads the file to my FTP site in a temp directory so I can quickly and easily share files with friends.  Just click it’s uploaded, and click the resulting URL is copied into the clipboard to be pasted into an e-mail, Word, blog post, etc.

To do this, I needed to upload a file to an FTP site.  There is a FtpWebRequest class in System.Net, but it was cryptic to use for just a simple file upload.  Our MSDN documents could definitely use more sample code!  Hopefully the new MSDN Wiki will help.  But after much searching, I came across BytesRoad.NetSuit, a free .NET library that was real easy to use!  It does more than just FTP, but here’s my code showing how easy it is to upload a file.

using System.IO;

using BytesRoad.Net.Ftp;

namespace CoadTools.Ftp

{

  public class FtpToools

  {

    public static void UploadFile(int Timeout, string FtpServer,

      string Username, string Password, string RemotePath,

      string LocalFile)

    {

      // get instance of the FtpClient

      FtpClient client = new FtpClient();

      // use passive mode

      client.PassiveMode = true;

      // connect to the specified FTP server

      client.Connect(Timeout, FtpServer, 21);

      client.Login(Timeout, Username, Password);

      // build the target file path

      string target = Path.Combine(RemotePath,

        Path.GetFileName(LocalFile)).Replace("\\", "/");

      // synchronously upload the file

      client.PutFile(Timeout, target, LocalFile);

    }

  }

}

Comments

  • Anonymous
    June 28, 2006
    PingBack from http://technote.thedeveloperside.com/?p=20

  • Anonymous
    November 26, 2006
    The comment has been removed

  • Anonymous
    December 12, 2006
    I would like that library too ! Thanks gu78@hotmail.com

  • Anonymous
    December 26, 2006
    Hi, Please send me the library too! christianembacher@gmx.at Thanks

  • Anonymous
    December 28, 2006
    Hey crew, yup, the link was broken as the target site was down and it looks tough to get elsewhere, so I've put the FTP library on my FTP.  Cheers! http://coad.net/blog/resources/BytesRoad.NetSuit_2_0.zip

  • Anonymous
    March 02, 2007
    Hello Because of much Spam, I would be pleased if you can delete my comment or edit it to another mail adress! (My comment is that with the christian embacher... mail adress) big thanks!

  • Anonymous
    March 13, 2007
    Hey Noah, I think this library is amazing, but there's not much documentation about it..... Do you have more code samples using this library? Thanks a lot

  • Anonymous
    March 29, 2007
    I completely agree that it's way too much work just to upload a file in .net. Thanks for the code, it worked great!

  • Anonymous
    February 21, 2008
    It's great... Thanks ^^ May the force be with you~

  • Anonymous
    April 05, 2008
    Hi, I am trying to use the above sample. However, while the connection to the server is successful, I get the below error when the PutFile command is executed: "An existing connection was forcibly closed by the remote host" Any help in this will be appreciated. Thanks.

  • Anonymous
    April 09, 2008
    Hey Noah,   I want to download certains files say Sam* is all the files that stsrt with 'Sam' how can i acheive this using GetFile() which doesn't have any masking parameter to take Thanks

  • Anonymous
    June 05, 2008
    Is It possible to upload a file from one server to another server (Not a local server).

  • Anonymous
    June 20, 2008
    4 Sachin This is bits of my code that I have taken out. This checks the file name if it ends with .txt and only executes the getfile command if the file has an .txt extension.


Dim idx as integer = 0 Dim item As FtpItem = items(idx)           If item.Name.ToString.EndsWith(".txt") Then       Try                        'fetching the file                        ftp.GetFile(timeout, dstpath, srcFilePath)       Catch ex As Exception       End Try End If

Havent tested but would suggest you try If item.Name.ToString.StartsWith("Sam") Then ftp.GetFile(timeout, dstpath, srcFilePath) End If

  • Anonymous
    August 12, 2008
    hi Noah, it's working fine.great job.Also can you please suggest me how to implement a progressbar along with this proccess which in fact shows the progress of the whole process, i.e. from connecting to finished uploading.i was indeed behind the backgroundworker for achieve this task but all in vain.can you help me out.please it's very urgent. the whole thing works except the progressbar. regards, Arun

  • Anonymous
    August 23, 2008
    Thanks! Did a Google search before even playing around with FTP on my own, found this article, downloaded the DLL, and bam!- it's working like a charm.

  • Anonymous
    September 04, 2008
    I always get Operation Failed on PutFile :/

  • Anonymous
    September 11, 2008
    Hey, When i tried this code i got the following ERROR - {"Timeout occurs." } Here it's my function where i pass the argument as: UploadFile(100,"ftp://tranzactftp.intergies.com","MGFTPU026","uq50fqlx!","ftp://tranzactftp.intergies.com/p1020/incoming/QA/",filePath);

  • Anonymous
    September 11, 2008
    Hey Now I am getting this ERROR - {"Operation failed." } At this location in Above code client.PutFile(Timeout, target, LocalFile);

  • Anonymous
    September 12, 2008
    Is there any documentaion on the web for this library???

  • Anonymous
    October 22, 2008
    Just saved me a lot of time, especially pointing out and mirroring the package.  Thanks a ton.

  • Anonymous
    November 04, 2008
    Can anyone give me source code? Please give me source.... Thanks

  • Anonymous
    November 04, 2008
    Can anyone give me source code? Please give me source on danish.ashfaq@hotmail.com Thanks

  • Anonymous
    March 17, 2009
    Can you please send me the dll file.. iamshaf@gmail.com thanks in advance

  • Anonymous
    March 18, 2009
    Shaf & Danish, use the bold link in the blog post above titled "BytesRoad.NetSuit" to download the source code and DLL.  Or do an internet search for the that term to find many other places to download it.

  • Anonymous
    April 01, 2009
    Hi , Do this code also feasible for multi-language files.As i am using the file name "~$فية المشروع.doc", but the current code is not working for this file. Thanks

  • Anonymous
    April 12, 2010
    If some are getting an operation failed message while upload, check that it might be a firewall (such as the one with an internet security package or windows) that blocks it. i solved this problem by switching my firewall to trusted connection or off in case while uploading and it worked for me.

  • Anonymous
    May 26, 2014
    The comment has been removed