0

我可以看到有两种类型的响应:

  1. 视窗
  2. Unix

例子

"08-25-12  06:52AM            139874418 3.03.06P13.12NB.rar"  
"-r-xr-xr-x   1 owner    group               1 Jun  3  1999 NotCurrentYear.txt"  

我需要解析它,我使用了以下逻辑:

AnalyzedFolder folderToBeAnalyzed = new AnalyzedFolder();
                folderToBeAnalyzed.Name = folder;
                Job.AnalyzedFolders.Add(folderToBeAnalyzed);


                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(textBoxFTPSite.Text + folder);
                request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
                request.Credentials = new NetworkCredential(textBoxFTPUserName.Text, textBoxFTPPassword.Text);

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();


                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);


                string[] outputlines = reader.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
                foreach (string info in outputlines)
                {
                    var tokens = info.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    string name;
                    string type;
                    string size;
                    DateTime dateModified;
                    string lsLine;

                    if (tokens.Length == 4) //WINDOWS
                    {
                        name = tokens[3];
                        if (tokens[2] == "<DIR>")
                        {
                            type = "D";
                            size = "";
                        }
                        else
                        {
                            type = "F";
                            size = tokens[2];
                        }
                        dateModified = DateTime.ParseExact(tokens[0] + " " + tokens[1], "MM-dd-yy h:mmtt", CultureInfo.InvariantCulture);
                        lsLine = info;
                        FTPFolderEntity entity = new FTPFolderEntity() { FolderName = folder, Name = name, Type = type, Size = size, DateModified = dateModified, LSLine = lsLine };
                        folderToBeAnalyzed.Entities.Add(entity);
                    }
                    else //UNIX
                    {
                    }
                }

问题是对于这个文件:

"11-15-12  10:02PM                  324 Copy (10) of 1040.txt.zip"

由于空格,逻辑失败。另外,就像这个错误一样,我怀疑我也可能遇到其他问题。任何人都可以指导我更好的解析方法吗?

4

2 回答 2

0

您可以在此处使用正则表达式来删除额外的Whitesapces

string info = "11-15-12  10:02PM                  324 Copy (10) of 1040.txt.zip";
string result = Regex.Replace(info, @"\s\s+", " ");

之后你会result得到

// result = "11-15-12 10:02PM 324 Copy (10) of 1040.txt.zip";

ADDED,如果您想限制您的令牌,假设第一个始终是日期,第二个是时间,其余是您的文件名或其他内容:

var tokens = Regex.Split(info, @"\s+");

var newTokens = new string[] 
{ 
    tokens[0], 
    tokens[1], 
    tokens[2],
    tokens[3] + ' ' + tokens[4] + ' ' + tokens[5] + ' ' + tokens[6]
};
于 2012-11-16T05:05:09.500 回答
0

您可以使用Regex进行拆分。

var tokens = Regex.Split(info, @"\s+");
于 2012-11-16T05:13:33.183 回答