Beispiel #1
0
        public void GetListFile()
        {
            try
            {
                string requestUrlString = Constants.FTP_URL + Constants.APP_NAME;
                if (Constants.APP_SUB.Length > 0)
                {
                    requestUrlString = Constants.FTP_URL + Constants.APP_NAME + "/" + Constants.APP_SUB;
                }
                //Tao ket noi FTP
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(requestUrlString);
                //Gan phuong thuc lay danh sach file
                request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
                //Dang nhap
                request.EnableSsl   = Constants.FTP_ENABLE_SSL;
                request.Credentials = new NetworkCredential(Constants.FTP_USERNAME, Constants.FTP_PASSWORD);
                request.KeepAlive   = false;

                if (Constants.FTP_ENABLE_SSL == true)
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(AcceptAllCertifications);
                }

                //Lay ket qua
                List <FileDownload> fileDownloadList = new List <FileDownload>();
                using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
                {
                    //Doc ket qua
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(responseStream))
                        {
                            //Xu ly ket qua
                            while (reader.EndOfStream == false)
                            {
                                FileDownload fileDownloadItem = new FileDownload();
                                if (fileDownloadItem.FromString(reader.ReadLine()) == false)
                                {
                                    fileDownloadList = null;
                                    break;
                                }
                                else
                                {
                                    fileDownloadItem.IndexID = fileDownloadList.Count + 1;
                                    fileDownloadList.Add(fileDownloadItem);
                                }
                            }
                            reader.Close();
                        }
                        responseStream.Close();
                    }
                    response.Close();
                }
                request.Abort();

                if (eventFTPDownloadFinshed != null)
                {
                    if (fileDownloadList != null && fileDownloadList.Count > 0)
                    {
                        eventFTPDownloadFinshed(UODownloadKind.FILE_LIST_SUCCESS, fileDownloadList);
                    }
                    else
                    {
                        eventFTPDownloadFinshed(UODownloadKind.FILE_LIST_FAIL, null);
                    }
                }
            }
            catch (Exception ex)
            {
                LogFile.Write("FTPDownload.GetListFile, ex: " + ex.ToString());
                if (eventFTPDownloadFinshed != null)
                {
                    eventFTPDownloadFinshed(UODownloadKind.FILE_LIST_FAIL, null);
                }
            }
        }