private async void OpenDirectoryOrDownloadFile() { FileStruct fileStruct = (FileStruct)lvFiles.SelectedItem; try { string name = fileStruct.Name; if (fileStruct.IsDirectory) { ftp.CurrentDirectory += "/" + name; listFiles.Clear(); (await Task.Run(() => ftp.ListDirectory(ftp.CurrentDirectory))).ForEach(x => listFiles.Add(x)); tbCurrentPath.Text = string.IsNullOrWhiteSpace(ftp.CurrentDirectory) ? "/" : ftp.CurrentDirectory; } else if (name == "..") { ftp.CurrentDirectory = ftp.ParentDirectory(ftp.CurrentDirectory); listFiles.Clear(); (await Task.Run(() => ftp.ListDirectory(ftp.CurrentDirectory))).ForEach(x => listFiles.Add(x)); tbCurrentPath.Text = string.IsNullOrWhiteSpace(ftp.CurrentDirectory) ? "/" : ftp.CurrentDirectory; } else { saveFileDialog.FileName = name; if (saveFileDialog.ShowDialog() == true) { await Task.Run(() => ftp.DownloadFile(ftp.CurrentDirectory, name, saveFileDialog.FileName)); sbStatus.MessageQueue.Enqueue(ftp.Status); } } } catch (Exception ex) { MessageBox.Show(ex.Message, ERROR_STRING, MessageBoxButton.OK, MessageBoxImage.Error); } }
private async void DeleteFileOrDirectory() { FileStruct fileStruct = (FileStruct)lvFiles.SelectedItem; try { if (fileStruct.IsDirectory) { await Task.Run(() => ftp.RemoveDirectory(ftp.CurrentDirectory, fileStruct.Name)); } else { await Task.Run(() => ftp.DeleteFile(ftp.CurrentDirectory, fileStruct.Name)); } sbStatus.MessageQueue.Enqueue(ftp.Status); listFiles.Clear(); (await Task.Run(() => ftp.ListDirectory(ftp.CurrentDirectory))).ForEach(x => listFiles.Add(x)); } catch (Exception ex) { MessageBox.Show(ex.Message, ERROR_STRING, MessageBoxButton.OK, MessageBoxImage.Error); } }
/// <summary> /// Парсинг записи, если FTP-сервер работает на Windows (WindowsStyle) /// </summary> /// <param name="record">Запись</param> /// <returns>Структура файла</returns> private FileStruct ParseFileStructWindowsStyle(string record) { FileStruct fileStruct = new FileStruct(); Match match = regexStyle.Match(record); fileStruct.CreateTime = DateTime.ParseExact(match.Groups["datetime"].Value, windowsDateTimeFormat, cultureInfo, DateTimeStyles.None).ToString("f"); if (match.Groups["sizeordir"].Value == "<DIR>") { fileStruct.IsDirectory = true; fileStruct.FileSize = NAME_FOLDER; fileStruct.IconType = TYPE_FOLDER; } else { fileStruct.IsDirectory = false; fileStruct.FileSize = AddSizeSuffix(long.Parse(match.Groups["sizeordir"].Value)); fileStruct.IconType = TYPE_FILE; } fileStruct.Name = match.Groups["name"].Value.Trim(); return(fileStruct); }