/// <summary> /// 删除文件 /// </summary> /// <param name="fileName"></param> public virtual void Delete(string fileName) { try { ftpConnection.RemoveFile(fileName); } catch { throw; } }
public bool removeRemotefile(string remoteFile) { bool ret = false; logger.pushOperation("FTP.removeRemotefile"); try { sFTP.RemoveFile(remoteFile); } catch (Exception e) { ret = false; logger.log("Erro ao remover arquivo do FTP: " + e.Message, Logger.LogType.ERROR, e, false); } finally { logger.releaseOperation(); } return(ret); }
public void ClearFtp(FtpConnection ftp, string directory) { ftp.SetCurrentDirectory(directory); var dirs = ftp.GetDirectories(); foreach (var dir in dirs) { if ((dir.Name != ".") && (dir.Name != "..")) { ClearFtp(ftp, dir.Name); //Recursive call ftp.RemoveDirectory(dir.Name); } } foreach (var file in ftp.GetFiles()) { ftp.RemoveFile(file.Name); } if (ftp.GetCurrentDirectory() != "/") { ftp.SetCurrentDirectory(".."); } }
//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; })); } } }