1

I'm having a bit of trouble with a program that I'm writing. The program checks the date created of a file on a remote ftp site, then if it matched today's date it will download the file the upload it to another ftp site. I get the following error when I run the program:

unhandled exception system.formatexception string was not recognized as a valid datetime

Here is the code I'm using to convert the ftp file created date to a datetime

/* Get the Date/Time a File was Created */
string fileDateTime = DownloadftpClient.getFileCreatedDateTime("test.txt");
Console.WriteLine(fileDateTime);
DateTime dateFromString = DateTime.Parse(fileDateTime, System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat);
Console.WriteLine(dateFromString);

Any ideas on what I'm doing wrong here?

  • What's the content of the `fileDateTime` string? – Richard Deeming Nov 29 '12 at 14:58
  • It should be the date the file was created on the ftp server. I can add the code for the class that line is referring to if it helps. – Humberto Perez Nov 29 '12 at 15:02
  • Yes, but what's the actual content of the string? Without a sample of the content, it's hard to see what you need to do to parse it. – Richard Deeming Nov 29 '12 at 15:12
  • The content of that string should be the result of the DownloadftpClient.getFileCreatedDateTime. Maybe I'm skipping a step between getting the timestamp of the file on the ftp server and tossing that result into a string? – Humberto Perez Nov 29 '12 at 15:17
  • OK, I understand **where** the content is coming from, but **what** is the content? Please post a sample! :) – Richard Deeming Nov 29 '12 at 15:18
  • Ok so you need to see what is actually getting passed into the string. Not sure how I can get that for you, sorry still relatively new at this. – Humberto Perez Nov 29 '12 at 15:24
  • You've got a `Console.WriteLine(fileDateTime);` in your code. What does that print? – Richard Deeming Nov 29 '12 at 15:25
  • Doesn't print anything. It just displays the unhandled exception system.formatexception string was not recognized as a valid datetime error – Humberto Perez Nov 29 '12 at 15:26
  • Without knowing where you got the `DownloadftpClient` from, or what the `getFileCreatedDateTime` method is returning, there's not enough information to provide any help. – Richard Deeming Nov 29 '12 at 15:36
  • I can see on the ftp server that the program successfully logged in and received the command. It returned 20121128194042 so it looks to me like that's what should be in fileDateTime string. What would you need me from me to help me troubleshoot this further? – Humberto Perez Nov 29 '12 at 15:41

1 Answers1

3

If the string returned from the sever is 20121128194042, then your code needs to be:

DateTime dateFromString = DateTime.ParseExact(fileDateTime, 
   "yyyyMMddHHmmss", // Specify the exact date/time format received
   System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat);

EDIT

The correct code for the getFileCreatedDateTime method should be:

public DateTime getFileCreatedDateTime(string fileName)
{
    try
    {
        ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + fileName);
        ftpRequest.Credentials = new NetworkCredential(user, pass);

        ftpRequest.UseBinary = true;
        ftpRequest.UsePassive = true;
        ftpRequest.KeepAlive = true;

        ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;
        ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();

        return ftpResponse.LastModified;
    }
    catch (Exception ex) 
    {
        // Don't like doing this, but I'll leave it here
        // to maintain compatability with the existing code:
        Console.WriteLine(ex.ToString());
    }

    return DateTime.MinValue;
}

(With help from this answer.)

The calling code then becomes:

DateTime lastModified = DownloadftpClient.getFileCreatedDateTime("test.txt");
Community
  • 1
  • 1
Richard Deeming
  • 29,830
  • 10
  • 79
  • 151
  • I tried adding that and still got the same error. I commented everything out after the Console.WriteLine(fileDateTime); just to see if what the output is and it's blank. So I'm guessing that nothing is getting tossed into the fileDateTime string and so there is nothing for the DateTime to parse. Not sure what the best course of action is here. – Humberto Perez Nov 29 '12 at 16:07
  • If it's returning an empty string, then either the file doesn't exist, or there's a bug with the `DownloadftpClient`. – Richard Deeming Nov 29 '12 at 16:10
  • I'm pretty sure there's a bug in the program. I have a rough idea on where I think the issue is but I'm not entirely sure. – Humberto Perez Nov 29 '12 at 16:22
  • Would it be out of line to post all the code and ask for help in finding the bug? – Humberto Perez Nov 29 '12 at 17:13
  • Is it something you've developed yourself? The closest version I can find is a CodeProject article posted in August by metastruct: http://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class – Richard Deeming Nov 29 '12 at 17:23
  • That's what I used to build off of. I made some tweaks since I needed a separate class for the upload and download server but otherwise it's pretty much the same. – Humberto Perez Nov 29 '12 at 17:29
  • It's probably better to ask the author then. You can use the "Comments and Discussions" at the bottom of the article to report the bug. – Richard Deeming Nov 29 '12 at 17:32
  • Will do. I appreciate the help – Humberto Perez Nov 29 '12 at 17:34
  • Looking at the code, it's expecting the date/time to be returned in the response stream, but it's actually returned in the `LastModified` property of the response: http://stackoverflow.com/questions/1040371/system-net-ftpwebrequest-getdatetimestamp-example – Richard Deeming Nov 29 '12 at 17:34