我可以看到有两种类型的响应:
- 视窗
- 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"
由于空格,逻辑失败。另外,就像这个错误一样,我怀疑我也可能遇到其他问题。任何人都可以指导我更好的解析方法吗?