C# 如何将FTP.ListDirectoryDetails的响应映射到对象
FTP.ListDirectoryDetails的输出通常是一个类似于下面给出的字符串: -rw-r--r--1 ftp 960 Aug 31 09:09 test1.xmlC# 如何将FTP.ListDirectoryDetails的响应映射到对象,c#,ftp,ftpwebrequest,C#,Ftp,Ftpwebrequest,FTP.ListDirectoryDetails的输出通常是一个类似于下面给出的字符串: -rw-r--r--1 ftp 960 Aug 31 09:09 test1.xml 如何将其转换为易于使用的对象?我找不到合适的解决方案将其转换为有用的对象,因此我创建了自己的对象。我希望下面的代码能帮助其他人。如果您有改进此代码的建议,请随时提供 下面是课堂: /// <summary> /// This class represents the file/directory informa
如何将其转换为易于使用的对象?我找不到合适的解决方案将其转换为有用的对象,因此我创建了自己的对象。我希望下面的代码能帮助其他人。如果您有改进此代码的建议,请随时提供 下面是课堂:
/// <summary>
/// This class represents the file/directory information received via FTP.ListDirectoryDetails
/// </summary>
public class ListDirectoryDetailsOutput
{
public bool IsDirectory { get; set; }
public bool IsFile {
get { return !IsDirectory; }
}
//Owner permissions
public bool OwnerRead { get; set; }
public bool OwnerWrite { get; set; }
public bool OwnerExecute { get; set; }
//Group permissions
public bool GroupRead { get; set; }
public bool GroupWrite { get; set; }
public bool GroupExecute { get; set; }
//Other permissions
public bool OtherRead { get; set; }
public bool OtherWrite { get; set; }
public bool OtherExecute { get; set; }
public int NumberOfLinks { get; set; }
public int Size { get; set; }
public DateTime ModifiedDate { get; set; }
public string Name { get; set; }
public bool ParsingError { get; set; }
public Exception ParsingException { get; set; }
/// <summary>
/// Parses the FTP response for ListDirectoryDetails into the Output object
/// An example input of a file called test1.xml:
/// -rw-r--r-- 1 ftp ftp 960 Aug 31 09:09 test1.xml
/// </summary>
/// <param name="ftpResponseLine"></param>
public ListDirectoryDetailsOutput(string ftpResponseLine)
{
try
{
string[] responseList = ftpResponseLine.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
string permissions = responseList[0];
//Get directory, currently only checking for not "-", might be beneficial to also check for "d" when mapping IsDirectory
IsDirectory = !string.Equals(permissions.ElementAt(0), '-');
//Get directory, currently only checking for not "-", might be beneficial to also check for r,w,x when mapping permissions
//Get Owner Permissions
OwnerRead = !string.Equals(permissions.ElementAt(1), '-');
OwnerWrite = !string.Equals(permissions.ElementAt(2), '-');
OwnerExecute = !string.Equals(permissions.ElementAt(3), '-');
//Get Group Permissions
GroupRead = !string.Equals(permissions.ElementAt(4), '-');
GroupWrite = !string.Equals(permissions.ElementAt(5), '-');
GroupExecute = !string.Equals(permissions.ElementAt(6), '-');
//Get Other Permissions
OtherRead = !string.Equals(permissions.ElementAt(7), '-');
OtherWrite = !string.Equals(permissions.ElementAt(8), '-');
OtherExecute = !string.Equals(permissions.ElementAt(9), '-');
NumberOfLinks = int.Parse(responseList[1]);
Size = int.Parse(responseList[4]);
string dateStr = responseList[5] + " " + responseList[6] + " " + responseList[7];
//Setting Year to the current year, can be changed if needed
dateStr += " " + DateTime.Now.Year.ToString();
ModifiedDate = DateTime.ParseExact(dateStr, "MMM dd hh:mm yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces);
Name = (responseList[8]);
ParsingError = false;
}
catch (Exception ex)
{
ParsingException = ex;
ParsingError = true;
}
}
}
//
///此类表示通过FTP.ListDirectoryDetails接收的文件/目录信息
///
公共类ListDirectoryDetailsOutput
{
公共bool IsDirectory{get;set;}
公共文件{
获取{return!IsDirectory;}
}
//所有者权限
公共bool OwnerRead{get;set;}
公共bool所有者写入{get;set;}
公共布尔所有者执行{get;set;}
//组权限
公共bool组读{get;set;}
公共bool GroupWrite{get;set;}
public bool GroupExecute{get;set;}
//其他权限
公共bool OtherRead{get;set;}
公共bool OtherWrite{get;set;}
公共bool OtherExecute{get;set;}
public int NumberOfLinks{get;set;}
公共整数大小{get;set;}
公共日期时间修改日期{get;set;}
公共字符串名称{get;set;}
公共布尔ParsingError{get;set;}
公共异常ParsingException{get;set;}
///
///将ListDirectoryDetails的FTP响应解析到输出对象中
///名为test1.xml的文件输入示例:
///-rw-r--r--1 ftp 960 Aug 31 09:09 test1.xml
///
///
public ListDirectoryDetailsOutput(字符串ftpResponseLine)
{
尝试
{
string[]responseList=ftpResponseLine.Split(新字符串[]{”“},StringSplitOptions.RemoveEmptyEntries);
字符串权限=响应列表[0];
//Get目录,当前仅检查非“-”,在映射IsDirectory时也检查“d”可能是有益的
IsDirectory=!string.Equals(permissions.ElementAt(0),'-');
//Get目录,当前仅检查非“-”,在映射权限时还检查r、w、x可能会有所帮助
//获取所有者权限
OwnerRead=!string.Equals(permissions.ElementAt(1),'-');
OwnerWrite=!string.Equals(permissions.ElementAt(2),'-');
OwnerExecute=!string.Equals(permissions.ElementAt(3),'-');
//获取组权限
GroupRead=!string.Equals(permissions.ElementAt(4),'-');
GroupWrite=!string.Equals(permissions.ElementAt(5),'-');
GroupExecute=!string.Equals(permissions.ElementAt(6),'-');
//获取其他权限
OtherRead=!string.Equals(permissions.ElementAt(7),'-');
OtherWrite=!string.Equals(permissions.ElementAt(8),'-');
OtherExecute=!string.Equals(permissions.ElementAt(9),'-');
NumberOfLinks=int.Parse(responseList[1]);
Size=int.Parse(responseList[4]);
字符串dateStr=responseList[5]+“”+responseList[6]+“”+responseList[7];
//将年份设置为当前年份,可以根据需要进行更改
dateStr+=“”+DateTime.Now.Year.ToString();
ModifiedDate=DateTime.ParseExact(dateStr,“MMM dd hh:mm yyyy”,CultureInfo.InvariantCulture,DateTimeStyles.AllowHiteSpaces);
名称=(响应列表[8]);
ParsingError=false;
}
捕获(例外情况除外)
{
ParsingException=ex;
ParsingError=true;
}
}
}
下面是我如何使用它:
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FTPURL);
request.Credentials = new NetworkCredential(FTPUsername, FTPPassword);
//Only required for SFTP
//request.EnableSsl = true;
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
List<ListDirectoryDetailsOutput> directories = new List<ListDirectoryDetailsOutput>();
string line = streamReader.ReadLine();
while (!string.IsNullOrEmpty(line))
{
directories.Add(new ListDirectoryDetailsOutput(line));
line = streamReader.ReadLine();
}
streamReader.Close();
//获取用于与服务器通信的对象。
FtpWebRequest=(FtpWebRequest)WebRequest.Create(FTPURL);
request.Credentials=新的网络凭据(FTPUsername、FTPPassword);
//仅SFTP需要
//request.enablesl=true;
request.Method=WebRequestMethods.Ftp.ListDirectoryDetails;
FtpWebResponse response=(FtpWebResponse)request.GetResponse();
StreamReader StreamReader=新的StreamReader(response.GetResponseStream());
列表目录=新列表();
string line=streamReader.ReadLine();
而(!string.IsNullOrEmpty(line))
{
添加(新的ListDirectoryDetailsOutput(行));
line=streamReader.ReadLine();
}
streamReader.Close();
请注意:仅当输入字符串的格式完全相同时,此操作才有效。否则,ParsingError将为true,异常将被捕获为ParsingException
注2:1年以上的文件格式为MMM dd yyyy,而不是MMM dd hh:mm。这需要对上面的代码进行一些调整。缺失的年份并不总是指当前年份(给出的代码假设不正确)。失踪的一年只是指过去一年内。当在4月份收到一份日期为12月20日的文件时,丢失的年份意味着去年,而不是今年。(谢谢Martin!)如果服务器根本不使用*nix样式列表,这将不起作用1)通常,超过1年的文件具有时间格式
MMM dd yyyy
,而不是MMM dd hh:mm
。3) 缺失的年份可以是最后一年,而不是当前年份,如果说现在是4月,列表中的日期是12月20日。4) 一些服务器支持带有空格的用户名/组名(特别是Windows)。这是一个很好的观点!我已经注意到了这将适用的确切格式,其他格式将需要一些调整。我已经添加了更多关于时间格式和缺少年份的注释。谢谢。我找不到那些。我将添加有关这些问题的链接。