2

I am trying to implement a windows service that will ping a FTP site and copy its contents once in every 3 hours.

This service has functions to

  1. List all files in the FTP site

  2. Copy one file

  3. Delete the copied file

  4. Repeats step 2 and 3 for all files in the site

curtisk
  • 19,950
  • 4
  • 55
  • 71
user302579
  • 21
  • 1
  • 1
  • 2
  • 3
    What is your question? Do you need to know how to do it? Do you want to know if anything already does it? Do you want to know what libraries will help you roll your own? What is your question? – Randolpho Mar 26 '10 at 14:21
  • Thanx for your reply, I need to know how to do it if u have already did is it possible to send the code or just guide me how to do it – user302579 Mar 26 '10 at 14:46
  • If the above operation is the only thing your service does, it should probably not be a service. Just implement the functionality you need as a standalone app, and put it in the task scheduler to run at the desired times. It's a more efficient use of both your time as a developer and system resources (by not having to keep yet another process running all the time). – Dathan Mar 26 '10 at 18:06

2 Answers2

3

Use FtpWebRequest. MSDN has samples for everything you need:

List all files

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/");
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

Copy one file

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
request.Method = WebRequestMethods.Ftp.DownloadFile;

Delete the copied file

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
request.Method = WebRequestMethods.Ftp.DeleteFile;
Mark Brackett
  • 84,552
  • 17
  • 108
  • 152
1

There are two classes which will be of great value to you for FTP. First, FtpWebRequest and second, FtpWebResponse. As for writing a windows service: this, and this should be helpful as well.

An example lifted from MSDN to delete a file:

public static bool DeleteFileOnServer(Uri serverUri)
{
    // The serverUri parameter should use the ftp:// scheme.
    // It contains the name of the server file that is to be deleted.
    // Example: ftp://contoso.com/someFile.txt.
    // 

    if (serverUri.Scheme != Uri.UriSchemeFtp)
    {
        return false;
    }
    // Get the object used to communicate with the server.
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
    request.Method = WebRequestMethods.Ftp.DeleteFile;

    FtpWebResponse response = (FtpWebResponse) request.GetResponse();
    Console.WriteLine("Delete status: {0}",response.StatusDescription);  
    response.Close();
    return true;
}

With a little bit of work you should be able to modify that to do every thing you need in terms of FTP Access.

Community
  • 1
  • 1
Nate
  • 30,286
  • 23
  • 113
  • 184