private static void DownloadDirectory(FtpConnection ftp, string p, string p_2) { string targetFolder = Path.GetFullPath(p_2); //string sourceFolder = ftp.GetCurrentDirectory() + '/' + p; DirectoryInfo localDir = new DirectoryInfo(targetFolder); if (localDir.Exists) { localDir.Delete(true); } localDir.Create(); ftp.SetCurrentDirectory(p); ftp.SetLocalDirectory(localDir.FullName); foreach (var file in ftp.GetFiles()) { string localFilename = localDir.FullName + Path.DirectorySeparatorChar + file.Name; if (File.Exists(localFilename)) { File.Delete(localFilename); } ftp.GetFile(file.Name, false); } foreach (var directory in ftp.GetDirectories()) { Directory.CreateDirectory(directory.Name); DownloadDirectory(ftp, directory.Name, targetFolder + Path.DirectorySeparatorChar + directory.Name); } ftp.SetCurrentDirectory(".."); }
/// <summary> /// Download Files /// </summary> private void DownloadFiles() { using (FtpConnection ftpConnection = this.CreateFtpConnection()) { if (!string.IsNullOrEmpty(this.WorkingDirectory)) { if (!Directory.Exists(this.WorkingDirectory)) { Directory.CreateDirectory(this.WorkingDirectory); } this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Setting Local Directory: {0}", this.WorkingDirectory)); FtpConnection.SetLocalDirectory(this.WorkingDirectory); } ftpConnection.LogOn(); if (!string.IsNullOrEmpty(this.RemoteDirectoryName)) { ftpConnection.SetCurrentDirectory(this.RemoteDirectoryName); } this.LogTaskMessage("Downloading Files"); if (this.FileNames == null) { FtpFileInfo[] filesToDownload = ftpConnection.GetFiles(); foreach (FtpFileInfo fileToDownload in filesToDownload) { this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Downloading: {0}", fileToDownload)); ftpConnection.GetFile(fileToDownload.Name, false); } } else { foreach (string fileName in this.FileNames.Select(item => item.ItemSpec.Trim())) { try { this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Downloading: {0}", fileName)); ftpConnection.GetFile(fileName, false); } catch (FtpException ex) { this.Log.LogError(string.Format(CultureInfo.CurrentCulture, "There was an error downloading file: {0}. The Error Details are \"{1}\" and error code is {2} ", fileName, ex.Message, ex.ErrorCode)); } } } } }
/// <summary> /// Upload Files /// </summary> private void UploadFiles() { if (this.FileNames == null) { this.Log.LogError("The required fileNames attribute has not been set for FTP."); return; } using (FtpConnection ftpConnection = this.CreateFtpConnection()) { this.LogTaskMessage("Uploading Files"); if (!string.IsNullOrEmpty(this.WorkingDirectory)) { this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Setting Local Directory: {0}", this.WorkingDirectory)); FtpConnection.SetLocalDirectory(this.WorkingDirectory); } ftpConnection.LogOn(); if (!string.IsNullOrEmpty(this.RemoteDirectoryName)) { this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Setting Current Directory: {0}", this.RemoteDirectoryName)); ftpConnection.SetCurrentDirectory(this.RemoteDirectoryName); } foreach (string fileName in this.FileNames.Select(item => item.ItemSpec)) { try { if (File.Exists(fileName)) { this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Uploading: {0}", fileName)); ftpConnection.PutFile(fileName); } } catch (FtpException ex) { this.Log.LogError(string.Format(CultureInfo.CurrentCulture, "There was an error uploading file: {0}. The Error Details are \"{1}\" and error code is {2} ", fileName, ex.Message, ex.ErrorCode)); } } } }
/// <summary> /// Upload Files /// </summary> private void UploadFiles() { if (this.FileNames == null) { this.Log.LogError("The required fileNames attribute has not been set for FTP."); return; } using (FtpConnection ftpConnection = this.CreateFtpConnection()) { this.LogTaskMessage("Uploading Files"); if (!string.IsNullOrEmpty(this.WorkingDirectory)) { this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Setting Local Directory: {0}", this.WorkingDirectory)); FtpConnection.SetLocalDirectory(this.WorkingDirectory); } ftpConnection.LogOn(); if (!string.IsNullOrEmpty(this.RemoteDirectoryName)) { this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Setting Current Directory: {0}", this.RemoteDirectoryName)); ftpConnection.SetCurrentDirectory(this.RemoteDirectoryName); } var overwrite = true; var files = new List <FtpFileInfo>(); if (!string.IsNullOrEmpty(this.Overwrite)) { if (!bool.TryParse(this.Overwrite, out overwrite)) { overwrite = true; } } if (!overwrite) { files.AddRange(ftpConnection.GetFiles()); } foreach (string fileName in this.FileNames.Select(item => item.ItemSpec)) { try { if (File.Exists(fileName)) { if (!overwrite && files.FirstOrDefault(fi => fi.Name == fileName) != null) { this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Skipped: {0}", fileName)); continue; } this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Uploading: {0}", fileName)); ftpConnection.PutFile(fileName); } } catch (FtpException ex) { this.Log.LogError(string.Format(CultureInfo.CurrentCulture, "There was an error uploading file: {0}. The Error Details are \"{1}\" and error code is {2} ", fileName, ex.Message, ex.ErrorCode)); } } } }
//main operations //download .pdf and print private void ftpOperations() { Int64 fileSize = 0; bool fileProblem = false; using (FtpConnection ftp = new FtpConnection(server.Text, login.Text, password.Text)) { try { ftp.Open(); loggingBox.Invoke(new Action(delegate() { loggingBox.Items.Add("Nawiązuję połączenie..."); })); saveToFile(); //connect to ftp and set remote and local directory ftp.Login(); ftp.SetCurrentDirectory("//" + packingStationNumber); ftp.SetLocalDirectory("C:\\tmp"); } catch (ThreadAbortException) { } catch (Exception e) { loggingBox.Invoke(new Action(delegate() { loggingBox.Items.Add("Błąd połączenia " + e); })); saveToFile(); btnStart.Invoke(new Action(delegate() { btnStart.Enabled = true; })); btnStop.Invoke(new Action(delegate() { btnStop.Enabled = false; })); startOperations.Abort(); } while (true) { btnStart.Invoke(new Action(delegate() { if (btnStart.Enabled == true) { btnStart.Enabled = false; } })); loggingBox.Invoke(new Action(delegate() { if (loggingBox.Items.Count > 2000) { loggingBox.Items.Clear(); } })); try { //search file on ftp foreach (var file in ftp.GetFiles()) { loggingBox.Invoke(new Action(delegate() { loggingBox.Items.Add("Pobieram plik " + file.Name); })); saveToFile(); foreach (var pdfFile in Directory.GetFiles("C:\\tmp")) { if (pdfFile == "C:\\tmp\\" + file.Name) { loggingBox.Invoke(new Action(delegate() { loggingBox.Items.Add("Znalazłem dubla: " + file.Name); })); saveToFile(); fileSize = new FileInfo("C:\\tmp\\" + file.Name).Length; fileProblem = true; } } if (!fileProblem) { ftp.GetFile(file.Name, false); } else if (fileSize > 40000) { MessageBox.Show("Twoja etykieta została pobrana już wcześniej i prawdopodobnie została wysłana. Jej nazwa to " + file.Name, "WARRNING", MessageBoxButtons.OK, MessageBoxIcon.Warning); loggingBox.Invoke(new Action(delegate() { loggingBox.Items.Add("Etykieta już jest na dysku: " + file.Name); })); saveToFile(); fileProblem = true; } else if (fileSize < 40000) { File.Delete("C:\\tmp\\" + file.Name); ftp.GetFile(file.Name, false); loggingBox.Invoke(new Action(delegate() { loggingBox.Items.Add("Etykieta w tmp ma zbyt mały rozmiar: " + file.Name + " i została znowu pobrana"); })); saveToFile(); fileProblem = false; } ftp.RemoveFile(file.Name); if (!fileProblem) { //run program to print .pdf if (sumatra_checkbox.Checked == false) { System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); FindProgram pdfProgramName = new FindProgram(); startInfo.FileName = (pdfProgramName.findPDFprogram("Adobe") + "\\Reader 11.0\\Reader\\AcroRd32.exe"); startInfo.Arguments = "/s /o /t C:\\tmp\\" + file.Name + " " + printer.Text; process.StartInfo = startInfo; loggingBox.Invoke(new Action(delegate() { loggingBox.Items.Add("Otwieram AR i wywołuję wydruk..."); })); saveToFile(); process.Start(); Thread.Sleep(4000); process.Close(); } else { System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); FindProgram pdfProgramName = new FindProgram(); startInfo.FileName = (pdfProgramName.findPDFprogram("SumatraPDF") + "\\SumatraPDF.exe"); startInfo.Arguments = "-silent C:\\tmp\\" + file.Name + " -print-settings fit -print-to " + printer.Text + " -exit-when-done"; process.StartInfo = startInfo; loggingBox.Invoke(new Action(delegate() { loggingBox.Items.Add("Otwieram SumatraPDF i wywołuję wydruk..."); })); saveToFile(); process.Start(); Thread.Sleep(2000); process.Close(); } } fileProblem = false; } } catch (ThreadAbortException) { } catch (Exception e) { loggingBox.Invoke(new Action(delegate() { loggingBox.Items.Add("Błąd przetwarzania plików " + e); })); saveToFile(); loggingBox.Invoke(new Action(delegate() { loggingBox.Items.Add("Ponowne nawiązanie połączenia"); })); ftp.Close(); ftp.Open(); ftp.Login(); ftp.SetCurrentDirectory("/" + packingStationNumber); ftp.SetLocalDirectory("C:\\tmp"); continue; } Thread.Sleep(750); loggingBox.Invoke(new Action(delegate() { loggingBox.Items.Add("[...]"); })); loggingBox.Invoke(new Action(delegate() { loggingBox.TopIndex = loggingBox.Items.Count - 1; })); } } }