开发者

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 开发者_JAVA技巧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.


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"
}


An annoying thing about the FTP standard is that it does not specify exactly how the directory listing should be formatted. In general, listings returned from *nix machines look more like *nix directory listings and those returned from Windows often look a lot like a DOS listing but you've got old FTP code forming the base of newer products so there is IBM-4690 and AS400 stuff, VMS, Oracle, Novell and so on.

So if you are trying to make something general purpose instead of for a specific server then you've got a lot of ugly parsing work to do. It might be worth your time to buy something but I don't have any recommendations.


This is my algorithm for parsing ListDirectoryDetails. I separated the File/Dir name, Attribute, Date Created, and Size into List. Hope this helps....

        FtpWebRequest _fwr = FtpWebRequest.Create(uri) as FtpWebRequest;
        _fwr.Credentials = cred;
        _fwr.UseBinary = true;
        _fwr.UsePassive = true;
        _fwr.KeepAlive = true;
        _fwr.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
        StreamReader _sr = new StreamReader(_fwr.GetResponse().GetResponseStream());

        List<object> _dirlist = new List<object>();
        List<object> _attlist = new List<object>();
        List<object> _datelist = new List<object>();
        List<long> _szlist = new List<long>();
        while (!_sr.EndOfStream)
        {
            string[] buf = _sr.ReadLine().Split(' ');
            //string Att, Dir;
            int numcnt = 0, offset = 4; ;
            long sz = 0;
            for (int i = 0; i < buf.Length; i++)
            {
                //Count the number value markers, first before the ftp markers and second
                //the file size.
                if (long.TryParse(buf[i], out sz)) numcnt++;
                if (numcnt == 2)
                {
                    //Get the attribute
                    string cbuf = "", dbuf = "", abuf = "";
                    if (buf[0][0] == 'd') abuf = "Dir"; else abuf = "File";
                    //Get the Date
                    if (!buf[i+3].Contains(':')) offset++;
                    for (int j = i + 1; j < i + offset; j++)
                    {
                        dbuf += buf[j];
                        if (j < buf.Length - 1) dbuf += " ";
                    }
                    //Get the File/Dir name
                    for (int j = i + offset; j < buf.Length; j++)
                    {
                        cbuf += buf[j];
                        if (j < buf.Length - 1) cbuf += " ";
                    }
                    //Store to a list.
                    _dirlist.Add(cbuf);
                    _attlist.Add(abuf);
                    _datelist.Add(dbuf);
                    _szlist.Add(sz);

                    offset = 0;
                    break;
                }
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜