private FileStruct ParseFileStructFromWindowsStyleRecord(string Record) { //Assuming the record style as // 02-03-04 07:46PM <DIR> Append FileStruct f = new FileStruct(); string processstr = Record.Trim(); string[] splitArray = processstr.Split(" \t".ToCharArray(), 2, StringSplitOptions.RemoveEmptyEntries); string dateStr = splitArray[0]; processstr = splitArray[1]; string timeStr = processstr.Substring(0, 7); processstr = (processstr.Substring(7, processstr.Length - 7)).Trim(); ConvertDate.Parse(dateStr, timeStr, out f.CreateTime); f.CreateTimeString = dateStr + timeStr; if (processstr.Substring(0, 5) == "<DIR>") { f.IsDirectory = true; processstr = (processstr.Substring(5, processstr.Length - 5)).Trim(); } else { f.IsDirectory = false; string[] strs = processstr.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries); long.TryParse(strs[0].Trim(), out f.Size); processstr = strs[1].Trim(); } f.Name = processstr; return f; }
/// <summary> /// Returns true if file exists /// | if file does not exist, method returns -1 /// | dummy method of FileExists /// </summary> /// <param name="remoteDirectory">directory on ftpServer</param> /// <param name="remoteFileName">filename on ftpServer</param> /// <param name="remFileSize">remote fileSize if file exists else -1</param> /// <returns>True if file exists else false</returns> public bool FileExists(string remoteDirectory, string remoteFileName, out long remFileSize) { var success = false; remFileSize = 0; var request = WebRequest.Create(new Uri("ftp://" + _host + ":" + Port + "/" + remoteDirectory + "/" + remoteFileName)) as FtpWebRequest; if (request != null) { request.Credentials = new NetworkCredential(UserName, Password); request.Timeout = TimeOut; request.UsePassive = true; request.KeepAlive = KeepAlive; request.Method = WebRequestMethods.Ftp.GetFileSize; try { using (FtpWebResponse response = request.GetResponse() as FtpWebResponse) { FileStruct fstruct = new FileStruct { Size = long.Parse(response.StatusDescription.Split(' ')[1].Replace("\r", "").Replace("\n", "")) }; response.Close(); remFileSize = fstruct.Size; success = true; } } catch { // ignored } } return success; }
/// <summary> /// Parses string to FileStruct /// </summary> /// <param name="ftpRecord">String including information of file/directory</param> /// <returns>FileStruct</returns> public FileStruct Parse(string ftpRecord) { FileStruct f = new FileStruct(); FileListStyle directoryListStyle = GuessFileListStyle(ftpRecord); if (directoryListStyle == FileListStyle.Unknown || ftpRecord == "") return f; f.Name = ".."; switch (directoryListStyle) { case FileListStyle.UnixStyle: f = ParseFileStructFromUnixStyleRecord(ftpRecord); break; case FileListStyle.WindowsStyle: f = ParseFileStructFromWindowsStyleRecord(ftpRecord); break; case FileListStyle.Unknown: throw new ArgumentOutOfRangeException(); default: throw new ArgumentOutOfRangeException(); } return f; }
private FileStruct ParseFileStructFromUnixStyleRecord(string Record) { //Assuming record style as // dr-xr-xr-x 1 owner group 0 Nov 25 2002 bussys FileStruct f = new FileStruct(); string processstr = Record.Trim(); f.Flags = processstr.Substring(0, 9); f.IsDirectory = (f.Flags[0] == 'd'); processstr = (processstr.Substring(11)).Trim(); _cutSubstringFromStringWithTrim(ref processstr, ' ', 0); //skip one part f.Owner = _cutSubstringFromStringWithTrim(ref processstr, ' ', 0); f.Group = _cutSubstringFromStringWithTrim(ref processstr, ' ', 0); long.TryParse(_cutSubstringFromStringWithTrim(ref processstr, ' ', 0), out f.Size); //skip one part f.CreateTime = DateTime.Parse(_cutSubstringFromStringWithTrim(ref processstr, ' ', 8)); f.Name = processstr; //Rest of the part is name return f; }