none
Connect to FTP Server using C#

    Question

  • I am new to C# and am having trouble with this. 

    I am using visual studio 2008. 

    I want to connect to a FTP Server.  I want it to be a console application and hard code the IP, Username and password into the application.  The goal is to schedule a task to run this application every day without user input to it.  The application will do other things once connected but I wanted to start with at least the connection to get me going.  Once I am connected I will need to look at a file that is there and obtain it's modified date....thats it.

    Can someone please help me out on this and provide source code for all files needed to run the application?

    Thank you so much.
    Friday, March 06, 2009 3:40 PM

Answers

  • i haven't tried this, but i assume you can simply do a DateTime.Parse() with the string returned by GetDateTimestamp. this will give you a DateTime which should be easy to compare to another DateTime.

    WM_HOPETHISHELPS
    -thomas woelfer
    http://www.die.de/blog
    • Marked as answer by Bin-ze Zhao Thursday, March 12, 2009 6:23 AM
    Tuesday, March 10, 2009 8:53 AM

All replies

  • take a look at the FtpWebRequest class.

    WM_HOPETHISHELPS
    -thomas woelfer
    http://www.die.de/blog
    Friday, March 06, 2009 4:27 PM
  • Here is what I have so far: 

                    //Create FTP request & login to server
                    FtpWebRequest request = FtpWebRequest.Create(ftpAddress + "/" + fileName) as FtpWebRequest;
                    request.Credentials = new NetworkCredential(userName, password);

                    //Get the DATE & TIME stamp of the file
                    request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
                    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                    Console.WriteLine(response.LastModified);

                    //Get DATE from above and chop off TIME


                    //Get Today's DATE


                    //Compare DATEs


    I created the connection and got the date and time stamp of the file but now I am having a hard time getting the date and time stamp into a string that I can chop off the time and just have the date to work with.

    I will need to compare the date in the time and date stamp of the file with the current date and return true of false if they match.

    ANY SUGGESTIONS?
    Monday, March 09, 2009 8:03 PM
  • i haven't tried this, but i assume you can simply do a DateTime.Parse() with the string returned by GetDateTimestamp. this will give you a DateTime which should be easy to compare to another DateTime.

    WM_HOPETHISHELPS
    -thomas woelfer
    http://www.die.de/blog
    • Marked as answer by Bin-ze Zhao Thursday, March 12, 2009 6:23 AM
    Tuesday, March 10, 2009 8:53 AM
  • I figured it out another way but thank you for the response.
    Tuesday, March 17, 2009 2:31 PM
  • Hi,

     

    I am also looking for the same thing, could you please share that code which you got? 

     

    Thanks in advance,

    Wednesday, May 19, 2010 10:40 PM
  • FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(FTPHostName + FTPFilePath);
    ftpRequest.Credentials = new NetworkCredential(FTPUsername, FTPPassword);
    ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;
    DateTime FtpFileLastModified = ((FtpWebResponse)ftpRequest.GetResponse()).LastModified);
    

    (not exactly tested yet. If necessary I'll post the working code)

    Wednesday, March 02, 2011 2:31 PM