如何解析?ftpWebRequest ListDirectorDetials

时间:2022-03-24 03:46:49

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

我在玩FtpWebRequest,我想知道如何格式化结果?

    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.

不确定我是否应该使用长或int来表示大小。我也不确定datetime是什么,如果它被创建、修改或其他什么。

3 个解决方案

#1


7  

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

#2


1  

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.

关于FTP标准的一个恼人的事情是它没有确切地指定如何格式化目录列表。一般来说,上市回来* nix机器看起来更像* nix目录列表和返回从Windows经常看起来很像一个DOS清单但你有老FTP的基础代码形成新的产品有ibm - 4690和小型机,vm,甲骨文,Novell等等。

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.

因此,如果您试图实现一些通用的目的,而不是针对某个特定的服务器,那么您就需要做很多糟糕的解析工作。也许你花时间买东西是值得的,但我没有任何建议。

#3


0  

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

这是我解析ListDirectoryDetails的算法。我将文件/Dir名称、属性、创建日期和大小分隔为列表。希望这有助于....

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

#1


7  

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

#2


1  

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.

关于FTP标准的一个恼人的事情是它没有确切地指定如何格式化目录列表。一般来说,上市回来* nix机器看起来更像* nix目录列表和返回从Windows经常看起来很像一个DOS清单但你有老FTP的基础代码形成新的产品有ibm - 4690和小型机,vm,甲骨文,Novell等等。

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.

因此,如果您试图实现一些通用的目的,而不是针对某个特定的服务器,那么您就需要做很多糟糕的解析工作。也许你花时间买东西是值得的,但我没有任何建议。

#3


0  

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

这是我解析ListDirectoryDetails的算法。我将文件/Dir名称、属性、创建日期和大小分隔为列表。希望这有助于....

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