Приложение, которое подключается к ftp-серверу и скачивает rar-архив - C#

Узнай цену своей работы

Формулировка задачи:

Необходимо было, создать просто консольное приложение, которое подключается к ftp-серверу и скачивает rar-архив в корневую папку где и сама программа, далее запускается winrar и распаковывает с заменой файлы в нужный каталог. Беда в том ,что после подключения к ftp-серверу ПК начинает постоянно пикать и программа и консоль выдает ерунду и отказывается работать. Подскажите пожалуйста, в чем мои ошибки. С библиотекой winrar не работал. Вот код:
Листинг программы
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Net;
  5. using System.Threading;
  6. namespace UpdateDataBaseFtpServer
  7. {
  8. class appStart
  9. {
  10. static void Main(string[] args)
  11. {
  12. Console.WriteLine("Start download rar-archive. Plase wait...");
  13. try
  14. {
  15. // Get the object used to communicate with the server.
  16. FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://xxx.xxx.xxx.xxx/NA_OSP/sklad.rar");
  17. request.Method = WebRequestMethods.Ftp.DownloadFile;
  18. // This example assumes the FTP site uses anonymous logon.
  19. request.Credentials = new NetworkCredential("xxxx", "xxxx");
  20. FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  21. Console.WriteLine("Logon OK!");
  22. Stream responseStream = response.GetResponseStream();
  23. StreamReader reader = new StreamReader(responseStream);
  24. Console.WriteLine(reader.ReadToEnd());
  25. Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
  26. reader.Close();
  27. response.Close();
  28. ProcessStartInfo ps = new ProcessStartInfo();
  29. ps.FileName = @"C:\Program Files\WinRAR\RAR.exe";
  30. ps.Arguments = @"e o+ C:\FTP\sklad.rar C:\RAB\SKLAD"; // key e and 0+ for with the replacement
  31. Console.WriteLine("Replace old files. Plase wait...");
  32. Process procCommand = Process.Start(ps);
  33. Console.WriteLine("It's done!");
  34. Thread.Sleep(5000);
  35. procCommand.WaitForExit();
  36. }
  37. catch (Exception ex)
  38. {
  39. Console.WriteLine("Eror: " + ex);
  40. Console.ReadLine();
  41. }
  42. }
  43. }
  44. }

Решение задачи: «Приложение, которое подключается к ftp-серверу и скачивает rar-архив»

textual
Листинг программы
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Net;
  5. using System.Threading;
  6.  
  7. namespace UpdateDataBaseFtpServer
  8. {
  9.     class appStart
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int Length = 2048;
  14.             Byte[] buffer = new Byte[Length];
  15.             Console.WriteLine("Start. Plase wait...");
  16.             Thread.Sleep(5000);
  17.             try
  18.             {
  19.                 string inputfilepath = @"C:\FTP\sklad.rar";
  20.                 string ftphost = "xxx.xxx.xxx.xxx";
  21.                 string ftpfilepath = "/NA_OSP/sklad.rar";
  22.                 string ftpfullpath = "ftp://" + ftphost + ftpfilepath;
  23.  
  24.                 using (WebClient request = new WebClient())
  25.                 {
  26.                     request.Credentials = new NetworkCredential("asdsf", "asfsa");
  27.                     Console.WriteLine("Logon OK!");
  28.                     byte[] fileData = request.DownloadData(ftpfullpath);
  29.                     Console.WriteLine("Start download!");
  30.                     using (FileStream file = File.Create(inputfilepath))
  31.                     {
  32.                         file.Write(fileData, 0, fileData.Length);
  33.                         file.Close();
  34.                     }
  35.                     Console.WriteLine("Download Complete!");
  36.                 }
  37.  
  38.                 ProcessStartInfo ps = new ProcessStartInfo();
  39.  
  40.                 ps.FileName = @"C:\Program Files\WinRAR\RAR.exe";
  41.  
  42.                 ps.Arguments = @"e o+ C:\FTP\sklad.rar C:\RAB\SKLAD";                                                                //
  43.  
  44.                 Console.WriteLine("Replace old files. Plase wait...");
  45.                 Process procCommand = Process.Start(ps);
  46.                 Console.WriteLine("It's done!");
  47.                 Thread.Sleep(5000);
  48.                 procCommand.WaitForExit();
  49.             }
  50.             catch (Exception ex)
  51.             {
  52.                 Console.WriteLine("Eror: " + ex);
  53.                 Console.ReadLine();
  54.             }
  55.         }
  56.     }
  57. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

9   голосов , оценка 3.667 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут