Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse this? ftpWebRequest ListDirectorDetials

I am playing around with FtpWebRequest and I am wondering how can I format the result?

    FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create("");
        ftp.Credentials = new NetworkCredential("", "");
       ftp.KeepAlive = true;
       ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
       WebResponse response = ftp.GetResponse();
       StreamReader reader = new StreamReader(response
                                       .GetResponseStream());

       string r = reader.ReadLine();
       response.Close();
       reader.Close();

I get results like this back

09-17-11  01:00AM               942038 my.zip

What would be a good way to parse this into like an object say something like

public Class Test()
{
   public DateTime DateCreated? {get; set;}
   public int/long  Size {get; set;}
   public string  Name {get; set;}
}

Not sure if I should use a long or int for the size. I am also not sure what the datetime is actually if it is created, or modified or whatever.

like image 772
chobo2 Avatar asked Sep 19 '11 19:09

chobo2


1 Answers

var value = "09-17-11  01:00AM               942038 my.zip";
var tokens = value.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (tokens.Length > 3)
{
    var test = new Test
    {
        DateCreated = DateTime.ParseExact(tokens[0] + tokens[1], "MM-dd-yyHH:mmtt", CultureInfo.InvariantCulture),
        Size = int.Parse(tokens[2]),
        Name = tokens[3]
    };

    // at this stage:
    // test.DateCreated = 17/09/2011 01:00AM
    // test.Size = 942038
    // test.Name = "my.zip"
}
like image 181
Darin Dimitrov Avatar answered Oct 11 '22 14:10

Darin Dimitrov