コード例 #1
0
ファイル: FtpClient.cs プロジェクト: 917457200/Photo
        /// <summary>
        /// 获取目录下的文件列表
        /// </summary>
        /// <param name="oFTPConfig"></param>
        /// <param name="alFileList"></param>
        /// <returns></returns>
        public bool GetRemoteFileList(FtpConfig oFTPConfig, ref ArrayList alFileList)
        {
            FtpLib    ftp = new FtpLib();
            string    strLocalFilePath  = @"" + oFTPConfig.strLocalFilePath;
            string    strRemoteFilePath = @"" + oFTPConfig.strRemoteFilePath;
            ArrayList myArraylist       = new ArrayList();

            try
            {
                ftp.port = int.Parse(oFTPConfig.strPort);
                ftp.Connect(oFTPConfig.strServer, oFTPConfig.strUserName, oFTPConfig.strPassword, int.Parse(oFTPConfig.strPort));
                ftp.ChangeDir(strRemoteFilePath);
                myArraylist = ftp.ListFiles();
                if (myArraylist.Count > 0)
                {
                    alFileList = GetFileName(myArraylist);
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return(false);
            }
            finally
            {
                ftp.Disconnect();
            }
        }
コード例 #2
0
ファイル: FtpClient.cs プロジェクト: 917457200/Photo
        /// <summary>
        /// 下载远程的单个文件
        /// </summary>
        /// <param name="strFileName"></param>
        /// <param name="oFTPConfig"></param>
        /// <returns></returns>
        public bool DownloadSingleRemoteFile(string strFileName, FtpConfig oFTPConfig)
        {
            string strLocalFileName  = @"" + oFTPConfig.strLocalFilePath + strFileName;
            string strRemoteFileName = @"" + oFTPConfig.strRemoteFilePath + strFileName;

            int perc, old_perc = 0;

            FtpLib ftp = new FtpLib();

            Console.WriteLine("【下拉文件】 {0} 开始从远程目录下载文件到本地 {1}", DateTime.Now.ToString(), strFileName);

            try
            {
                ftp.Connect(oFTPConfig.strServer, oFTPConfig.strUserName, oFTPConfig.strPassword, int.Parse(oFTPConfig.strPort));
                ftp.ChangeDir(oFTPConfig.strRemoteFilePath);
                ftp.OpenDownload(strFileName, false);

                while (true)
                {
                    perc = ftp.DoDownload();

                    // No need to report progress everytime we get some bytes
                    // because it causes a flickery effect on the screen in most    cases.
                    if (perc > old_perc)
                    {
                        Console.Write("\r【下拉进度】 Ftp Complete: {0}%", perc);
                        Console.Out.Flush();
                    }

                    // is the download done?
                    if (perc == 100)
                    {
                        Console.WriteLine(" ");
                        File.Copy(@"./" + strFileName, strLocalFileName, true);
                        File.Delete(@"./" + strFileName);
                        break;
                    }

                    old_perc = perc;
                }

                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return(false);
            }
            finally
            {
                ftp.Disconnect();
            }
        }