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

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

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

Необходимо было, создать просто консольное приложение, которое подключается к ftp-серверу и скачивает rar-архив в корневую папку где и сама программа, далее запускается winrar и распаковывает с заменой файлы в нужный каталог. Беда в том ,что после подключения к ftp-серверу ПК начинает постоянно пикать и программа и консоль выдает ерунду и отказывается работать. Подскажите пожалуйста, в чем мои ошибки. С библиотекой winrar не работал. Вот код:
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading;
 
namespace UpdateDataBaseFtpServer
{
    class appStart
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Start download rar-archive. Plase wait...");
            try
            {
                // Get the object used to communicate with the server.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://xxx.xxx.xxx.xxx/NA_OSP/sklad.rar");
                request.Method = WebRequestMethods.Ftp.DownloadFile;
 
                // This example assumes the FTP site uses anonymous logon.
                request.Credentials = new NetworkCredential("xxxx", "xxxx");
 
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
 
                Console.WriteLine("Logon OK!");
 
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);
                Console.WriteLine(reader.ReadToEnd());
 
                Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
 
                reader.Close();
                response.Close();
 
                ProcessStartInfo ps = new ProcessStartInfo();
 
                ps.FileName = @"C:\Program Files\WinRAR\RAR.exe";
 
                ps.Arguments = @"e o+ C:\FTP\sklad.rar C:\RAB\SKLAD";   // key e and 0+ for with the replacement
 
                Console.WriteLine("Replace old files. Plase wait...");
                Process procCommand = Process.Start(ps);
                Console.WriteLine("It's done!");
                Thread.Sleep(5000);
                procCommand.WaitForExit();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Eror: " + ex);
                Console.ReadLine();
            }
        }
    }
}

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

textual
Листинг программы
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading;
 
namespace UpdateDataBaseFtpServer
{
    class appStart
    {
        static void Main(string[] args)
        {
            int Length = 2048;
            Byte[] buffer = new Byte[Length];
            Console.WriteLine("Start. Plase wait...");
            Thread.Sleep(5000);
            try
            {
                string inputfilepath = @"C:\FTP\sklad.rar";
                string ftphost = "xxx.xxx.xxx.xxx";
                string ftpfilepath = "/NA_OSP/sklad.rar";
                string ftpfullpath = "ftp://" + ftphost + ftpfilepath;
 
                using (WebClient request = new WebClient())
                {
                    request.Credentials = new NetworkCredential("asdsf", "asfsa");
                    Console.WriteLine("Logon OK!");
                    byte[] fileData = request.DownloadData(ftpfullpath);
                    Console.WriteLine("Start download!");
                    using (FileStream file = File.Create(inputfilepath))
                    {
                        file.Write(fileData, 0, fileData.Length);
                        file.Close();
                    }
                    Console.WriteLine("Download Complete!");
                }
 
                ProcessStartInfo ps = new ProcessStartInfo();
 
                ps.FileName = @"C:\Program Files\WinRAR\RAR.exe";
 
                ps.Arguments = @"e o+ C:\FTP\sklad.rar C:\RAB\SKLAD";                                                                // 
 
                Console.WriteLine("Replace old files. Plase wait...");
                Process procCommand = Process.Start(ps);
                Console.WriteLine("It's done!");
                Thread.Sleep(5000);
                procCommand.WaitForExit();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Eror: " + ex);
                Console.ReadLine();
            }
        }
    }
}

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

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