示例#1
0
 private void CloseResponce()
 {
     if (Responce != null)
     {
         Responce.Close();
     }
     //if (Request!=null)
     //{
     //    Request.Abort();
     //}
 }
示例#2
0
        private void downloadDirectory(string createDir, string remoteDirectory)
        {
            lock (remoteLV)
            {
                try
                {
                    Directory.CreateDirectory(createDir);
                    FtpWebRequest Request = (FtpWebRequest)WebRequest.Create(remoteDirectory);
                    Request.Credentials = new NetworkCredential(login, password);
                    Request.Method      = WebRequestMethods.Ftp.ListDirectory;
                    Request.Proxy       = null;
                    FtpWebResponse Responce;
                    Stream         stream;
                    StreamReader   streamReader;
                    string         ans;
                    string[]       files = new string[0];
                    try
                    {
                        Responce     = (FtpWebResponse)Request.GetResponse();
                        stream       = Responce.GetResponseStream();
                        streamReader = new StreamReader(stream, Encoding.Default);
                        ans          = streamReader.ReadToEnd().Replace("\r\n", "\n");
                        files        = ans.Split('\n');
                        streamReader.Close();
                        Responce.Close();
                        stream.Close();
                        for (int i = 0; i < files.Length - 1; i++)
                        {
                            Request             = (FtpWebRequest)WebRequest.Create(remoteDirectory + files[i]);
                            Request.Credentials = new NetworkCredential(login, password);
                            Request.Method      = WebRequestMethods.Ftp.DownloadFile;
                            Request.Proxy       = null;
                            Responce            = (FtpWebResponse)Request.GetResponse();
                            FileStream outputStream = new FileStream(createDir + "\\" + files[i], FileMode.Create);
                            stream = Responce.GetResponseStream();
                            int    bufferSize = 1024;
                            int    readCount;
                            byte[] buffer = new byte[bufferSize];

                            readCount = stream.Read(buffer, 0, bufferSize);
                            while (readCount > 0)
                            {
                                outputStream.Write(buffer, 0, readCount);
                                readCount = stream.Read(buffer, 0, bufferSize);
                            }
                            outputStream.Close();
                        }
                        streamReader.Close();
                        Responce.Close();
                        stream.Close();
                    }
                    catch (WebException ex)
                    {
                        Responce = (FtpWebResponse)ex.Response;
                        if (Responce.StatusCode != FtpStatusCode.ActionNotTakenFileUnavailable)
                        {
                            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
                        }
                    }
                    Request             = (FtpWebRequest)WebRequest.Create(remoteDirectory);
                    Request.Credentials = new NetworkCredential(login, password);
                    Request.Method      = WebRequestMethods.Ftp.ListDirectoryDetails;
                    Request.Proxy       = null;
                    Responce            = (FtpWebResponse)Request.GetResponse();
                    stream       = Responce.GetResponseStream();
                    streamReader = new StreamReader(stream);
                    ans          = streamReader.ReadToEnd().Replace("\r\n", "\n");
                    string[] directories = ans.Split('\n');
                    streamReader.Close();
                    List <string> lviList = new List <string>();
                    for (int i = 2; i < directories.Length - 1; i++)
                    {
                        string   name  = string.Empty;
                        string[] parts = directories[i].Split(' ');
                        for (int j = 23; j < parts.Length; j++)
                        {
                            name += parts[j];
                            if (j != parts.Length - 1)
                            {
                                name += ' ';
                            }
                        }
                        lviList.Add(name);
                    }
                    Responce.Close();
                    stream.Close();
                    for (int i = 0; i < lviList.Count; i++)
                    {
                        for (int j = 0; j < files.Length - 1; j++)
                        {
                            if (lviList[i] == files[j])
                            {
                                lviList.RemoveAt(i);
                                i--;
                                break;
                            }
                        }
                    }
                    for (int i = 0; i < lviList.Count; i++)
                    {
                        downloadDirectory(createDir + "\\" + lviList[i], remoteDirectory + lviList[i] + '/');
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
                }
            }
        }
示例#3
0
 private void deleteDirectory(string remoteDirectory)
 {
     lock (remoteLV)
     {
         try
         {
             FtpWebRequest Request = (FtpWebRequest)WebRequest.Create(remoteDirectory);
             Request.Credentials = new NetworkCredential(login, password);
             Request.Method      = WebRequestMethods.Ftp.ListDirectory;
             Request.Proxy       = null;
             FtpWebResponse Responce;
             Stream         stream;
             StreamReader   streamReader;
             string         ans;
             string[]       files = new string[0];
             try
             {
                 Responce     = (FtpWebResponse)Request.GetResponse();
                 stream       = Responce.GetResponseStream();
                 streamReader = new StreamReader(stream, Encoding.Default);
                 ans          = streamReader.ReadToEnd().Replace("\r\n", "\n");
                 files        = ans.Split('\n');
                 streamReader.Close();
                 Responce.Close();
                 stream.Close();
                 for (int i = 0; i < files.Length - 1; i++)
                 {
                     Request             = (FtpWebRequest)WebRequest.Create(remoteDirectory + files[i]);
                     Request.Credentials = new NetworkCredential(login, password);
                     Request.Method      = WebRequestMethods.Ftp.DeleteFile;
                     Request.Proxy       = null;
                     Responce            = (FtpWebResponse)Request.GetResponse();
                 }
                 streamReader.Close();
                 Responce.Close();
                 stream.Close();
             }
             catch (WebException ex)
             {
                 Responce = (FtpWebResponse)ex.Response;
                 if (Responce.StatusCode != FtpStatusCode.ActionNotTakenFileUnavailable)
                 {
                     MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
                 }
             }
             Request             = (FtpWebRequest)WebRequest.Create(remoteDirectory);
             Request.Credentials = new NetworkCredential(login, password);
             Request.Method      = WebRequestMethods.Ftp.ListDirectoryDetails;
             Request.Proxy       = null;
             Responce            = (FtpWebResponse)Request.GetResponse();
             stream       = Responce.GetResponseStream();
             streamReader = new StreamReader(stream);
             ans          = streamReader.ReadToEnd().Replace("\r\n", "\n");
             string[] directories = ans.Split('\n');
             streamReader.Close();
             List <string> lviList = new List <string>();
             for (int i = 2; i < directories.Length - 1; i++)
             {
                 string   name  = string.Empty;
                 string[] parts = directories[i].Split(' ');
                 for (int j = 23; j < parts.Length; j++)
                 {
                     name += parts[j];
                     if (j != parts.Length - 1)
                     {
                         name += ' ';
                     }
                 }
                 lviList.Add(name);
             }
             Responce.Close();
             stream.Close();
             for (int i = 0; i < lviList.Count; i++)
             {
                 deleteDirectory(remoteDirectory + lviList[i] + '/');
             }
             Request             = (FtpWebRequest)WebRequest.Create(remoteDirectory.Substring(0, remoteDirectory.Length - 1));
             Request.Credentials = new NetworkCredential(login, password);
             Request.Method      = WebRequestMethods.Ftp.RemoveDirectory;
             Request.Proxy       = null;
             Responce            = (FtpWebResponse)Request.GetResponse();
             Responce.Close();
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
         }
     }
 }